Powershell add-content line breaks

后端 未结 3 350
死守一世寂寞
死守一世寂寞 2021-01-18 01:53

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

echo $server \"Uptime: \" $uptime.days \", Days\" $uptime.hours \", Hours\" $up         


        
相关标签:
3条回答
  • 2021-01-18 02:19

    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.

    0 讨论(0)
  • 2021-01-18 02:36

    how about

    [IO.File]::AppendAllText($testfile,"abc",[System.Text.Encoding]::UTF8)
    
    0 讨论(0)
  • 2021-01-18 02:40

    The simplest way to sidestep any problem PowerShell might be putting into the line breaks would be to avoid using the providers.

    By using [IO.File]::WriteAllText to write the file, you should be able to avoid the linebreaks that come from PowerShell. The only caveat is that [IO.File]::WriteAllText doesn't understand PowerShell paths, so you'll need to pass it an absolute path.

    Hope this helps,

    0 讨论(0)
提交回复
热议问题