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.

>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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap