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!
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
More Posts related to Powershell,
- How to upgrade PowerShell on Mac
- How to install AWS CLI 2 on Windows 11 using PowerShell
- How to switch to Powershell on Mac Terminal
- How to connect to Microsoft Exchange Online using PowerShell
- How to Check PowerShell Version? [Windows/Mac/Linux]
- How to Comment out Code in PowerShell Script
- How to Identify installed PowerShell version
- How to install PowerShell on macOS
- Open PowerShell Terminal in Visual Studio Code (VSCode)
- PowerShell ps1 script is not digitally signed, you cannot run this script on the current system
- Update Powershell Using Command Line
- PowerShell 1..10 foreach Example
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- List of PowerShell Cmdlet Commands for Mac
- Help or Man equivalent in PowerShell
- PowerShell SubString with Examples
- PowerShell: How to Get Folder Size
- PowerShell Traditional For Loop Example
- PowerShell Switch Statement with Examples
- PowerShell Concatenate String Examples
- PowerShell For Each Loop Examples
- PowerShell: Grep Command Alternative - Select-String
- How to delete a file using PowerShell [Windows/macOS]
- Fix: nano is not recognized as an internal or external command - Windows PowerShell
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
More Posts:
- How to quit or exit SFTP Prompt on Terminal - Linux
- Steps to Delete or Deactivate Instagram Account - HowTos
- Ways to Convert Integer or int to Long in Java - Java
- Java StringJoiner Class With Examples - Java
- How to Cut Copy and Paste in Nano Text Editor - Linux
- Hide Bluetooth icon on Mac Menu Bar macOS Ventura 13 - MacOS
- How to show SSL Certificate details using cURL Command - cURL
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program - Powershell