简体   繁体   English

Powershell Out-File输出全部在同一行

[英]Powershell Out-File output all on the same line

I have a powershell script that populates a variable, $Users , from the contents of a text file using the Get-Content cmdlet. 我有一个Powershell脚本,该脚本使用Get-Content cmdlet从文本文件的内容填充变量$Users I then want to append this information to the end of a different text file using Out-File. 然后,我想使用Out-File将此信息附加到其他文本文件的末尾。 However, currently the output is appended all in a row. 但是,当前输出被全部附加到一行。 What I need is for each string to be on it's own line. 我需要的是每个字符串都单独显示。

I have tried piping the variable into the Write-Output cmdlet and it displays correctly on the screen, but when I redirect it from Write-Output back to the Out-File cmdlet it appends the information all in a row again. 我尝试将变量通过管道传递到Write-Output cmdlet中,并在屏幕上正确显示,但是当我将其从Write-Output重定向回Out-File cmdlet时,它将再次连续附加所有信息。

$Users = Get-Content "C:\Users\XXXX\Desktop\Password Reset\Users5.txt"<br>
Out-File -InputObject $Users -FilePath "C:\Users\XXXX\Desktop\Password Reset\RefUsers.txt"

If it was me I would use Add-Content for this with a pipe. 如果是我,我会在管道上使用Add-Content

$Users = Get-Content -Path "C:\Users\XXXX\Desktop\Password Reset\Users5.txt"
$Users | Add-Content -Path "C:\Users\XXXX\Desktop\Password Reset\RefUsers.txt"

Pay attention to encoding. 注意编码。 Add-Content uses ascii by default I believe. 我相信默认情况下, Add-Content使用ascii。 Also if you are not doing anything with the data you can skip the variable all together. 另外,如果您对数据不做任何操作,则可以一起跳过变量。

GC "C:\Users\XXXX\Desktop\Password Reset\Users5.txt" | 
    AC "C:\Users\XXXX\Desktop\Password Reset\RefUsers.txt"

Gc being an alias for Get-Content and Ac for Add-Content Gc是Get-Content的别名,Ac是Add-Content的别名

Here is how my final script turned out: 这是我最终脚本的结果:

$Users = Get-Content "C:\\Users\\XXXX\\Desktop\\Password Reset\\Users5.txt" $ Users =获取内容“ C:\\ Users \\ XXXX \\ Desktop \\ Password Reset \\ Users5.txt”
Foreach($U in $Users){ Foreach(在$ Users中为$ U){
Add-Content "C:\\Users\\XXXX\\Desktop\\Password Reset\\RefUsers1.txt" "`n$U" 添加内容“ C:\\ Users \\ XXXX \\ Desktop \\ Password Reset \\ RefUsers1.txt”“`n $ U”
} }

Thanks Matt! 谢谢马特!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM