Use Get-Content to Store Entire Text File Contents in Variable in PowerShell. The Get-Content cmdlet gets the item's content at the specified location. You can use this command to view the text in a file or the content of a function. The following command gets the content of a file C:\New\test.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
To import data from a text file into PowerShell we use Get-Content CmdLet. In the example above we read the text file and save the result in variable $computers that will be used later. The syntax is very simple we call Get-Content and provide value to the Path parameter where our text file has been located.
The Get-Content cmdlet uses the Path parameter to specify the LineNumbers. txt file and displays the content in the PowerShell console.
On a side note, in PowerShell 3.0 you can use the Get-Content
cmdlet with the new Raw switch:
$text = Get-Content .\file.txt -Raw
To get the entire contents of a file:
$content = [IO.File]::ReadAllText(".\test.txt")
Number of lines:
([IO.File]::ReadAllLines(".\test.txt")).length
or
(gc .\test.ps1).length
Sort of hackish to include trailing empty line:
[io.file]::ReadAllText(".\desktop\git-python\test.ps1").split("`n").count
(see detailed explanation here)
$text = Get-Content $filePath | Out-String
The IO.File.ReadAllText
didn't work for me with a relative path, it looks for the file in %USERPROFILE%\$filePath
instead of the current directory (when running from Powershell ISE at least):
$text = [IO.File]::ReadAllText($filePath)
$text = Get-Content $filePath -Raw
One more approach to reading a file that I happen to like is referred to variously as variable notation or variable syntax and involves simply enclosing a filespec within curly braces preceded by a dollar sign, to wit:
$content = ${C:file.txt}
This notation may be used as either an L-value or an R-value; thus, you could just as easily write to a file with something like this:
${D:\path\to\file.txt} = $content
Another handy use is that you can modify a file in place without a temporary file and without sub-expressions, for example:
${C:file.txt} = ${C:file.txt} | select -skip 1
I became fascinated by this notation initially because it was very difficult to find out anything about it! Even the PowerShell 2.0 specification mentions it only once showing just one line using it--but with no explanation or details of use at all. I have subsequently found this blog entry on PowerShell variables that gives some good insights.
One final note on using this: you must use a drive designation, i.e. ${drive:filespec}
as I have done in all the examples above. Without the drive (e.g. ${file.txt}
) it does not work. No restrictions on the filespec on that drive: it may be absolute or relative.
Get-Content grabs data and dumps it into an array, line by line. Assuming there aren't other special requirements than you listed, you could just save your content into a variable?
$file = Get-Content c:\file\whatever.txt
Running just $file
will return the full contents. Then you can just do $file.Count
(because arrays already have a count method built in) to get the total # of lines.
Hope this helps! I'm not a scripting wiz, but this seemed easier to me than a lot of the stuff above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With