Powershell read and write file (Get-Content, Set-Content): How to use

powershell

When I read a file into a variable

$file = Get-Content myfile.utf8

and then write this same content into a new file

Set-Content -Encoding utf8 mynewfile.txt $file

Powershell automatically adds a return and linefeed to the end of the file.

How can I get the content of a file and write it back without Powershell adding anything?

Best Answer

Here is a workaround from this site.

Use

[System.IO.File]::WriteAllText($path, $text)
Related Topic