如何在PowerShell中删除换行符CRLF?

时间:2018-11-15 12:51:27

标签: powershell

我写了脚本,但是我不明白,如何删除换行符?

Get-WmiObject win32_computersystem name | select name -ExpandProperty Name | Out-File C:\Distrib\test.txt 
    $a=Get-Content C:\Distrib\test.txt
    $b=$a.split('-')
    $c=$b[$b.Length-1]
    $c = $c | foreach {"S"+$_} 
    $c | Out-File C:\Distrib\test.txt

1 个答案:

答案 0 :(得分:0)

请参见Get-Help Out-File并猜测参数-NoNewline的作用是什么?

您可以将脚本缩短为

'S'+(Get-WmiObject win32_computersystem).Name.Split('-')[-1] |
    Out-File C:\Distrib\test.txt -NoNewline -Enc default

没有-Encoding,您将获得带有BOM的Unicode