header image

Write text to a file with Powershell

Posted by: | September 25, 2017 Comments Off on Write text to a file with Powershell |

PowerShell is all about working with objects but sooner or later you’ll need to write text to a file with Powershell.

You have two options. The *-Content cmdlets and Out-File

PS> Get-Command *-Content

CommandType     Name 
 -----------     ---- 
 Cmdlet          Add-Content 
 Cmdlet          Clear-Content 
 Cmdlet          Get-Content 
 Cmdlet          Set-Content


 PS> Get-Command *-File

CommandType     Name 
 -----------     ---- 
 Cmdlet          Out-File 
 Cmdlet          Unblock-File

Lets start with Out-File

Out-File simply sends output to a file. Its the equivalent of the redirection operator but with parameters.

PS> Get-Process -Name power* | Out-File -FilePath gp1.txt 
 PS> Get-Content gp1.txt

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    672      33   124272     144548       1.88   6112   1 powershell 
    767      31    86756     105336       2.64  11984   1 powershell

Whatever comes down the pipeline is redirected to the specified file instead of being displayed on screen.

By default Out-File (like redirection) will overwrite an existing file. You can append data to the file

PS> Get-Process -Name power* | Out-File -FilePath gp1.txt -Append 
 PS> Get-Content gp1.txt

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    672      33   124272     144548       1.88   6112   1 powershell 
    767      31    86756     105336       2.64  11984   1 powershell

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    673      33   124272     144580       1.88   6112   1 powershell 
    798      32    73980      92564       2.64  11984   1 powershell

To protect an existing file that you don’t want overwriting IF it exists use the –NoClobber parameter

PS> Get-Process -Name power* | Out-File -FilePath gp1.txt -NoClobber 
 Out-File : The file 'C:\test\gp1.txt' already exists. 
 At line:1 char:28 
 + Get-Process -Name power* | Out-File -FilePath gp1.txt -NoClobber 
 +                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo          : ResourceExists: (C:\test\gp1.txt:String) [Out-File], IOException 
    + FullyQualifiedErrorId : NoClobber,Microsoft.PowerShell.Commands.OutFileCommand

You can overwrite a read only file with the –Force parameter.

By default Out-File uses unicode encoding. Other encodings are available

Unknown
String
Unicode
BigEndianUnicode
UTF8
UTF7
UTF32
ASCII
Default
OEM

This will be important if the file will be read by something other than PowerShell.

The *-Content cmdlets are different in that they expect strings

PS> Get-Process -Name power* | Set-Content -Path gp2.txt 
 PS> Get-Content -Path gp2.txt 
 System.Diagnostics.Process (powershell) 
 System.Diagnostics.Process (powershell)

Set-Content and Add-Content use ASCII encoding by default. The other encodings are available

ASCII Uses the encoding for the ASCII (7-bit) character set.
BigEndianUnicode Encodes in UTF-16 format using the big-endian byte order.
BigEndianUTF32 Encodes in UTF-32 format using the big-endian byte order.
Default Encodes using the default value: ASCII.
Byte Encodes a set of characters into a sequence of bytes.
String Uses the encoding type for a string.
Unicode Encodes in UTF-16 format using the little-endian byte order.
UTF7 Encodes in UTF-7 format.
UTF8 Encodes in UTF-8 format.
Unknown The encoding type is unknown or invalid; the data can be treated as binary.

We can modify our previous  example

PS> Get-Process -Name power* | Out-String | Set-Content -Path gp2.txt 
 PS> Get-Content -Path gp2.txt

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    627      34   156396     177492       3.00   6112   1 powershell 
    726      32    71640      91540       3.17  11984   1 powershell

Set-Content will overwrite the content of a file or create a new file. Add-Content appends data to a file

PS> Get-Process -Name power* | Out-String | Add-Content -Path gp2.txt 
 PS> Get-Content -Path gp2.txt

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    627      34   156396     177492       3.00   6112   1 powershell 
    726      32    71640      91540       3.17  11984   1 powershell


 Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName 
 -------  ------    -----      -----     ------     --  -- ----------- 
    676      34   156436     177648       3.05   6112   1 powershell 
    744      32    71604      91540       3.27  11984   1 powershell

The help files for these cmdlets show so other examples.

NOTE: In PowerShell v6 the encodings will be standardised.

under: PowerShell

Comments are closed.

Categories