When trying to output a text file to the screen using PowerShell and the following command:
$configText = get-content "C:\Helloworld.txt"
Write-Host "Helloworld:"
Write-Host $configText
The output was:
Helloworld:
Line 1 Line 2 Line 3
I.e. no carriage returns that were in the text file. To get the output to be as per the text file (with each line being on a separate line), you can amend your script to be the following:
$configText = get-content "C:\Helloworld.txt" | Out-String
Write-Host "Helloworld:"
Write-Host $configText
This then output the expected:
Helloworld:
Line 1
Line 2
Line 3
I believe this is because as default get-content actually returns an array of strings, and displays them all on the same line when you do a Write-Host (even if you specify $OFS = "'r'n") so you have to get the content as a single string.
To find out more about "Out-String", see:
Out-String
http://technet.microsoft.com/en-us/library/dd315365.aspx
Using the Get-Content Cmdlet
http://technet.microsoft.com/en-us/library/ee176843.aspx
What is $OFS
http://blogs.msdn.com/b/powershell/archive/2006/07/15/what-is-ofs.aspx
|
|
||||||
|
This Month
Month Archive
About Me
Login
|
PowerShell: Get-Content From a File with Carriage Returns
No comments found.
|
Search
Recent Articles
Favourite blogs
|
||||
|
||||||
