Powershell Out-File output all on the same line

powershell

I have a powershell script that populates a variable, $Users, from the contents of a text file using the Get-Content cmdlet. I then want to append this information to the end of a different text file using 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.

$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"

Best Answer

If it was me I would use Add-Content for this with a pipe.

$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. 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