Powershell add-content line breaks

I am trying to figure out how to eliminate line breaks when using add-content

echo $server "Uptime: " $uptime.days ", Days" $uptime.hours ", Hours" $uptime.minutes ", Minutes" | add-content $output_file 

Basically, I tried to get the server uptime to go to a text file, and when I do this, the output goes out

 HOSTNAME Uptime: , 2 Days 2 , Hours 15 , Minutes 

I reviewed this question: Powershell will replace line break loss

I also switched from using out-file-append to add-content, however they both give similar results, can someone shed some light on how I can eliminate the interruptions?

+7
source share
3 answers

I think you want to have one line with information, and then:

 "$server Uptime: $($uptime.days) Days, $($uptime.hours) Hours, $($uptime.minutes) Minutes" | add-content $output_file 

If each item should be on a separate line, you can add `n

 "$server Uptime`n$($uptime.days) Days`n$($uptime.hours) Hours`n$($uptime.minutes) Minutes" | add-content $output_file 

Another possibility is to use -f , which is sometimes more readable:

 "$server Uptime: {0} Days, {1} Hours, {2} Minutes" -f $uptime.days, $uptime.hours, $uptime.minutes | add-content $output_file 

Updating echo is an alias for Write-Output ( Get-Alias -name echo ), which in your case creates an array of objects. This array is passed to Add-Content ; each object is stored in its own line.

+10
source

The easiest way to get around any problem that PowerShell could put in line breaks would be to avoid using providers.

Using the [IO.File] :: WriteAllText file to write a file, you should be able to avoid the lines that appear in PowerShell. The only caveat is that [IO.File] :: WriteAllText does not understand PowerShell paths, so you need to pass an absolute path to it.

Hope this helps,

+1
source

What about

 [IO.File]::AppendAllText($testfile,"abc",[System.Text.Encoding]::UTF8) 
+1
source

All Articles