PowerShell: How to Use Out-File with No Output Header


Often you may want to use the Out-File cmdlet in PowerShell to send output to a specific file.

By default, PowerShell will include a header row when outputting the data to a file.

However, you can use -ExpandProperty to prevent a header row from being included in the output:

Get-ChildItem | Select -ExpandProperty Name | Out-File my_files.txt

This particular example will export the names of the files in the current folder to a text file named my_files.txt and will not include a header row in the file.

The following example shows how to use this syntax in practice.

Example: How to Use Out-File with No Output Header in PowerShell

Suppose that we would like to export the names of all the files in the current folder to a text file.

We can use the ls command to first view all of the files in the current folder:

We can see that there are three text files in the folder.

Suppose that we attempt to use the Out-File cmdlet with the following syntax to output the names of these files to a text file named my_files.txt:

Get-ChildItem | Select Name | Out-File my_files.txt

The following screenshot shows how to use this syntax in practice:

When we use the Get-Content cmdlet to view the contents of the text file, we can see that a header row is included in the file by default.

To prevent a header row from being exported, we can use -ExpandProperty option as follows:

Get-ChildItem | Select -ExpandProperty Name | Out-File my_files.txt

The following screenshot shows how to use this syntax in practice:

PowerShell out-file no header

When we use the Get-Content cmdlet to view the contents of the text file, we can see that no header row is included in the output file.

Note: You can find the complete documentation for the Out-File cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to List Files in Directory by Date
PowerShell: How to List All Files in Directory to Text File
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply