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.
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?
Subreddit
Post Details
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...