PowerShell: How to Write Output to a File

If you want to redirect the output of a PowerShell command or a Script to a file, you can make use of the > and >> operators.


OperatorDescription
>Will create a new file if it doesn't exist. Overwrites the file with new output.
>>To append output to the end of an existing file. Creates a new file if it doesn't exist.

Let's take a look at a few examples.


Example 1: Write Command Output to a new file

PS> echo "Hello! Welcome to Code2care.org" > message.txt

cat ./message.txt
Hello! Welcome to Code2care.org

Example 2: Append Command Output to an existing file

PS> echo "PowerShell is Fun!" >> message.txt

cat ./message.txt
Hello! Welcome to Code2care.org
PowerShell is Fun!

Example 3: Write Script Output to a file

PS> pwsh ./powershell-script.ps1 > logs.txt

cat ./logs.txt
The above 3 lines of code are commented out!

PowerShell Write Output to file example

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!