Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

PowerShell: Out-File versus Set-Content

📅 2012-Mar-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ powershell ⬩ 📚 Archive

If you want to pipe some content to a file, which of these two cmdlets would you use: Out-File or Set-Content?

Out-File is just the same as the output redirection operator (>) you use in any other shell. It formats the output as if it were writing to the console and instead of writing to the console, it writes to the file you specify.

This behaviour of Out-File can be sometimes problematic. If the content you are piping through Out-File has lines of text that are longer than the console width, Out-File will break them at the console width into a newline. So, you might end up with files where the long input lines have been broken down into multiple smaller lines of length equal to the console width.

If you do not want such meddling in your content, stick to using Set-Content to pipe output to files. Set-Content does not mess with the content and writes the textual content pristinely to file.

Tried with: PowerShell 2.0


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧