Table of Contents
Using Out-File
Cmdlet
Use the Out-File
cmdlet to redirect PowerShell output to a file.
1 2 3 |
Get-Process -Name chrome | Out-File -FilePath E:\Test\OuputFile.txt |
The Get-Process is used to retrieve all the processes that are currently running on our machine (local computer). We can filter this information by specifying parameters; for instance, we used the -Name
parameter to get all chrome
processes currently running. Without parameters, this cmdlet will get all the processes currently running on the local computer.
The Out-File
cmdlet is used to send the information received from the previous process to a given file. This cmdlet implicitly uses the formatting system of PowerShell to write to the given file and gets the exact display representation as the PowerShell console.
Out-File
wrote the received information to the OutputFile.txt
, which we specified using the -FilePath
parameter name. Remember, the above command will create the .txt
file if it does not exist and will overwrite it if it exists. So, use the -Append
parameter if you do not want to lose your existing content or use -NoClobber
to avoid overwriting the file.
This Out-File
cmdlet is recommended if we are required to use parameters to filter the information; otherwise, the redirection option (we will learn it in a while) is better. For example, we can use the following parameters with Out-File
.
Parameter | Description |
---|---|
-FilePath |
It is used to specify the location and file’s name. |
-Append |
It is used to append output to an existing file. |
-Force |
We can overwrite a read-only file using this parameter. |
-Width |
It limits the number of characters on every line (the default limit is 80 characters). |
-WhatIf |
It is used to run in the test mode. |
-NoClobber |
It does not allow us to overwrite the file. |
-NoNewLine |
It does not permit writing newline characters. |
Using Redirection Operators
Use redirection operators >
and >>
to redirect PowerShell output to a file.
1 2 3 |
Get-Process -Name chrome > E:\Test\OuputFile.txt |
In the previous section, we have already learned about the Get-Process
cmdlet and -Name
parameter. Here, we used the redirection operator >
to write the PowerShell output to the file. It will create the OutputFile.txt
if it is not present in the given directory and overwrite it if it is there.
Using the >
operator, we would only be able to write to the given file if everything works fine because it is used to the Success Only
stream. However, there are multiple variations of redirection operators to write different streams. So let’s look at them with a description and equivalent cmdlet.
Redirection Operator | Description | Equivalent Cmdlet |
---|---|---|
> |
It is used to write a Success only stream. |
Write-Output |
2> |
It is used to write an Error only stream. |
Write-Error |
3> |
We can use it to write a Warning only stream. |
Write-Warning |
4> |
It is used to write Verbose only steam. |
Write-Verbose |
5> |
It is used to write Debug only steam. |
Write-Debug |
6> |
It is used to write Information only steam. |
Write-Information |
*> |
All |
Don’t worry if you are looking for a redirect operator to append a file; we have you covered. We can use another redirection operator (>>
) to append output in the specified file.
1 2 3 |
Get-Process >> E:\Test\OuputFile.txt |
Further reading:
Using StreamWriter
.Net Class
To redirect PowerShell output to a file:
- Instantiate the
System.IO.StreamWriter
class using theNew-Object
cmdlet. - Use the
Get-Process
cmdlet to get information about all the currently running processes. - Send the retrieved information from the previous step to the
ForEach-Object
cmdlet using a pipeline (|
). - Now,
ForEach-Object
iterates over every process object and write to the specified file. - Lastly, use the
.Close()
method to close theStreamWriter
object.
1 2 3 4 5 |
$streamWriter = New-Object System.IO.StreamWriter("E:\Test\Output.txt") Get-Process | ForEach-Object {$streamWriter.WriteLine($_)} $streamWriter.Close() |
In this example, the New-Object
cmdlet is used to create a new instance of the System.IO.StreamWriter class, which is used to write text (string type data) to a stream. Next, the StreamWriter
object is initialized with the path to the output file as an argument.
The Get-Process
cmdlet is used to retrieve a list of processes, which is piped to the ForEach-Object
cmdlet. The ForEach-Object
cmdlet iterates through each process object and writes it to the output file using the WriteLine
method of the StreamWriter
object.
Finally, the Close
method of the StreamWriter
object is called to close the stream and release any associated resources. This code will create a file named Output.txt
in the E:\Test directory and write the output of the Get-Process
command to the file.
Note: The
StreamWriter
class is part of the .NET Framework and can be used in PowerShell to write text to a file or other stream. It provides more advanced features than the built-in redirection operators, such as the ability to specify the encoding of the output, append to an existing file, control the buffer size and flush the buffer to the stream.
Using Add-Content
Cmdlet
Use the Add-Content
cmdlet to redirect PowerShell output to a file.
1 2 3 |
Get-Process -Name chrome | Add-Content E:\Test\FileOutput.txt |
This command retrieved a list of processes using the Get-Process
cmdlet and appended the output to the end of the FileOutput.txt
file in the E:\Test directory. If the given file does not exist, it will be created.
You can also use the -Encoding
parameter to specify the output file’s encoding and the -NoNewline
parameter to prevent the cmdlet from adding a new line character at the end of an output.
1 2 3 |
Get-Process -Name chrome | Add-Content E:\Test\FileOutput.txt -Encoding UTF8 -NoNewline |
The Add-Content cmdlet is useful when we want to write the PowerShell command’s output to a file, but do not need to perform advanced operations such as seeking or reading from the file. In addition, it is advantageous when we want to append the output to an existing file, as it does not overwrite its contents.
Using Set-Content
Cmdlet
Use the Set-Content
cmdlet to redirect PowerShell output to a file.
1 2 3 |
Get-Process -Name chrome | Set-Content E:\Test\FileOutput.txt |
In the previous section, we have already learned about the Add-Content
cmdlet and -Encoding
parameter. This command is similar to the Add-Content
, which retrieves a list of processes using the Get-Process
cmdlet and writes the output to the FileOutput.txt
file located in the E:\Test directory.
But it will overwrite the content if the file already exists. So again, like the Add-Content
cmdlet, we can also use the -Encoding
parameter. The Set-Content
cmdlet is beneficial when we want to replace the contents of an existing file, as it overwrites the file’s contents.