New filters on the Home Feed, take a look!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

16
Strange behaviour with New-Item and Get-Content
Post Body

Hi r/powershell,

I have an issue that I don't understand. I have a function that gets the content of a file. The functions first tests if the file exist with Test-Path, and if it does not, it creates the file with New-Item. It then proceed to get its content with Get-Content and sends it to the pipeline. I would expect two possible outcomes: Either it send $null to the pipeline, since the file was just created and is empty, or it sends the content of the file to the pipeline. However, if I the file does not yet exist, it send the fileinfo of the file to the pipeline.

Here is the function:

Function Import-History {

    if (-not (Test-Path -Path $script:path_history)) {

        New-Item -Path $script:path_history -Type File -Value $null

    }

    Get-Content -Path $script:path_history | ConvertFrom-Json 

}

$path_history = Join-Path -Path $PSScriptRoot -ChildPath 'History\History.json'

Import-History

If the file does not exist, the output is:

PS P:\> Import-History


    Directory: P:\History


Mode                LastWriteTime         Length Name                                                                                                                                                                       
----                -------------         ------ ----                                                                                                                                                                       
-a----        9/30/2017   2:02 PM              0 History.json                                                                                                                                                               



PS P:\> 

If I run it again:

PS P:\> Import-History

PS P:\> 

Why is that?

Author
Account Strength
90%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,540
Link Karma
3,347
Comment Karma
1,193
Profile updated: 4 days ago
Posts updated: 6 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago