Send output to a file. When you wish to specify parameters, use Out-File instead of the redirection operator (>).

      Out-File [-filePath] string [[-encoding] string]
            [-append] [-width int] [-inputObject psobject]
               [-force] [-noClobber] [-whatIf]
                  [-confirm] [CommonParameters]
   -filePath path
       The path to the output file. 

   -encoding string
       The character encoding used in the file. 
       'Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode',
       'Default', or 'OEM'. The default is Unicode (UTF-16LE).
       Choosing 'Default' will set to the system ANSI code page. 
       Choosing 'OEM' will set to the OEM code page identifier for the OS.

       Add the output to the end of an existing file, instead of replacing the file contents.
       You need to ensure the -encoding matches any existing content in the target file

   -width int
       The number of characters in each line of output. Any additional
       characters are truncated, not wrapped. If you omit this parameter, 
       the width is determined by the characteristics of the host. The default 
       for the PowerShell.exe host is 80 (characters).

   -inputObject psobject
       The object to be written to file. {may be piped}
       A command, expression or variable that contains the objects.

       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or override a files read-only attribute, but will not change file permissions.

       Do not overwrite (replace the contents) of an existing file. 
       By default Out-File will overwrite an existing file without warning.
       If both -Append and -NoClobber are specified, the output is appended.

       Describe what would happen if you executed the command without
       actually executing the command.

       Prompt for confirmation before executing the command.

If the current location is a registry key, then -filepath must either be specified as filesystem::yourfilename.txt or use a full path C:\docs\yourfile.txt

Truncated lines – if your pipeline includes a Select-Object, use -ExpandProperty to prevent the output from being truncated.

The final part of displaying output is a hidden background call to an Output cmdlet, by default as the last part of the execution process PowerShell calls the default output cmdlet which is typically Out-Host.

Under Windows, Out-File and > / >> create Unicode UTF-16LE - files by default.
This can be configured via the $PSDefaultParameterValues preference variable.
Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8.
An alternative is to use Add-Content which will create ANSI files by default.


Send a list of files to FileList.txt:

PS C:\> $files = GetChildItem C:\demo\*
PS C:\> $files | Select-Object fullname | Out-File C:\demo\filelist.txt

If you need to select multiple properties then save them using Export-CSV:

PS C:\> $files = GetChildItem C:\demo\*
PS C:\> $files | Select-Object fullname, lastWriteTime | Export-CSV C:\demo\filelist.csv -NoTypeInformation

Send a list of processes to process.txt:

PS C:\> Get-Process | Out-File -filepath C:\docs\process.txt
PS C:\> Get-Process | Out-File C:\docs\process.txt

The same thing, but storing the list of processes in a variable first and truncating the output at 50 characters:

PS C:\> $a = get-process
Out-File -filepath C:\docs\process.txt -inputobject $a -width 50

Export a text string (not a PowerShell object) to an ASCII CSV file:

PS C:> Echo "Col1,Col2"`r`n123,456" | Out-File demo.csv -encoding ASCII

Save a registry key to file :

PS C:\> Set-Location hklm:\software
PS HKLM:\software> Get-Acl ODBC | Out-File c:\docs\acl.txt -width 200

"Imagine sitting at your computer and, in less than a second, searching the full text of every book ever written” ~ Eric Schmidt

Related PowerShell Cmdlets

Out-Default - Send output to default.
Export-CSV - Export a PowerShell object to a comma-separated values (CSV) file.
Out-GridView - Send output to an interactive table.
Out-Host - Send the pipelined output to the host.
Out-Null - Send output to null.
Out-Printer - Send the output to a printer.
Out-String - Send output to the pipleline as strings.
Tee-Object - Send input objects to two places.
Equivalent bash commands: redirection - Redirection and Process Substitution, cat > file2.txt – Redirect standard input into a file.

Copyright © 1999-2024 SS64.com
Some rights reserved