Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 have been trying to get the uptime of a server to go to a text file, and when I do this the output comes out

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

I looked at this question: Powershell replace lose line breaks

Also I went from using out-file -append to add-content, however both produce similar results, can someone shed to some light on how I can eliminate the breaks?

like image 463
powershellrookie Avatar asked Mar 30 '11 06:03

powershellrookie


1 Answers

I guess you want to have one line with the info, then:

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

If every item should be on separate line, you might add `n

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

Other 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

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

like image 85
stej Avatar answered Sep 19 '22 11:09

stej