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.
| Operator | Description |
|---|---|
> | 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!

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!