PowerShell Get-Content vs [System.IO.File]::ReadAllText

The difference between

Get-Content "FileName.txt"

and

[System.IO.File]::ReadAllText("FileName.txt")

That first one returns array of lines in the file and second returns one string for whole file.

There are couple of other ways to achieve second behavior with Get-Content as well

According to this in PowerShell 3 you can do that using new parameter Raw

Get-Content "FileName.txt" -Raw

The solution that works in PowerShell 2:

Get-Content "FileName.txt" | Out-String
Advertisements

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

2 Responses to PowerShell Get-Content vs [System.IO.File]::ReadAllText

  1. kewwwa says:

    Another non negligible difference is memory usage. Unfortunately “Get-Content” won’t release opened file from memory…

  2. Thanks for this!

    Consider to use [IO.File]::ReadAllBytes() instead, if line endings are important, or if you’re handling a binary file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s