Skip to content
PowerShell & CMDBeginner

How to Export File Names and Combine Multiple File Contents into One Text File Using PowerShell

Quick Answer PowerShell provides built-in commands to export file names from a folder or combine the actual contents of multiple files into a single text fil...

BI
Bison Technical Team Enterprise IT specialists
Updated 6 min read 1 total views
Structured technical guidanceSafety notes included where requiredSources cited when applicable

Quick Answer

PowerShell provides built-in commands to export file names from a folder or combine the actual contents of multiple files into a single text file. You can use the Get-ChildItem, Get-Content, and Out-File cmdlets to perform these tasks without installing additional software.

Depending on what you want to achieve, use one of the following commands:

Advertisement

1. Export Only File Names

Get-ChildItem -File | Select-Object -ExpandProperty Name | Out-File files.txt

2. Export File Names Including Subfolders

Get-ChildItem -File -Recurse | Select-Object -ExpandProperty FullName | Out-File files.txt

3. Combine Contents of All Text Files into One File

Get-ChildItem -Filter *.txt -File | ForEach-Object {
    "=== $($_.Name) ==="
    Get-Content -LiteralPath $_.FullName
} | Out-File combined.txt -Encoding utf8

The following guide explains these commands and shows how to customize them for different requirements.

1. Open PowerShell in the Target Folder

Before running any command, open PowerShell in the folder whose files you want to process.

  1. Open File Explorer.
  2. Navigate to the required folder.
  3. Click the address bar.
  4. Type powershell and press Enter.
  5. A PowerShell window will open with that folder as the current working directory.

You can also open PowerShell normally and navigate to the folder using the Set-Location command.

Set-Location "D:\MyFolder"

Replace D:\MyFolder with the actual folder path.

2. Export a List of File Names to a Text File

If you only need the names of files inside a folder without their contents, use the following command:

Get-ChildItem -File | Select-Object -ExpandProperty Name | Out-File files.txt

This command creates a text file named files.txt in the current folder and writes the names of all files into it.

Include Files from All Subfolders

To include files located inside subfolders as well, use the -Recurse parameter.

Get-ChildItem -File -Recurse | Select-Object -ExpandProperty FullName | Out-File files.txt

This exports the complete paths of files, making it easier to identify their locations.

Export Only Specific File Types

You can filter the results to include only particular file extensions.

Example: Export all PDF files

Get-ChildItem -Filter *.pdf -File | Select-Object -ExpandProperty Name | Out-File pdf-files.txt

Example: Export all Word documents

Get-ChildItem -Filter *.docx -File | Select-Object -ExpandProperty Name | Out-File word-files.txt

Example: Export all files with their paths, including subfolders

Get-ChildItem -Filter *.pdf -File -Recurse | Select-Object -ExpandProperty FullName | Out-File pdf-files.txt

3. Combine Multiple Text Files into One Text File

If you want to collect the actual text or data inside multiple text files and merge everything into one file, use Get-Content together with Out-File.

Run the following command:

Get-ChildItem -Filter *.txt -File | ForEach-Object {
    "=== $($_.Name) ==="
    Get-Content -LiteralPath $_.FullName
    ""
} | Out-File combined.txt -Encoding utf8

This command performs the following operations:

  1. Get-ChildItem -Filter *.txt -File finds all text files in the current folder.
  2. ForEach-Object processes each file individually.
  3. $($_.Name) displays the name of the file being processed.
  4. Get-Content reads the actual contents of each file.
  5. Out-File writes all collected content into combined.txt.

The resulting file will contain the contents of all matching text files, with a filename separator before each section.

Include Text Files from Subfolders

If your folder contains multiple subfolders and you want to combine text files from all of them, use the following command:

Get-ChildItem -Filter *.txt -File -Recurse | ForEach-Object {
    "=== $($_.FullName) ==="
    Get-Content -LiteralPath $_.FullName
    ""
} | Out-File combined.txt -Encoding utf8

This command searches the current folder and all its subfolders, reads every matching text file, and combines their contents into a single output file.

Important: If the destination file already exists inside the source folder, exclude it from the input list to avoid including previous output when running the command again.

$outputFile = Join-Path (Get-Location) "combined.txt"

Get-ChildItem -Filter *.txt -File -Recurse |
    Where-Object { $_.FullName -ne $outputFile } |
    ForEach-Object {
        "=== $($_.FullName) ==="
        Get-Content -LiteralPath $_.FullName
        ""
    } | Out-File -FilePath $outputFile -Encoding utf8

4. Combine Specific File Types into One File

PowerShell can also combine files with extensions other than .txt, provided they contain readable text.

Change the file filter according to the required extension.

File Type PowerShell Filter
Text files *.txt
Log files *.log
CSV files *.csv
Markdown files *.md
Configuration files *.ini
PowerShell scripts *.ps1
XML files *.xml
JSON files *.json

For example, to combine all log files into one text file:

Get-ChildItem -Filter *.log -File | ForEach-Object {
    "=== $($_.Name) ==="
    Get-Content -LiteralPath $_.FullName
    ""
} | Out-File combined-logs.txt -Encoding utf8

Note that changing the file extension filter does not convert binary files into text. Files such as PDF, DOCX, XLSX and images require appropriate extraction or conversion tools to retrieve their actual contents.

5. Export File Names and Additional File Information

Sometimes you may need more than just file names. PowerShell can export additional information such as file size, creation date and modification date.

Use the following command to export file details into a CSV file:

Get-ChildItem -File -Recurse |
    Select-Object Name, FullName, Length, CreationTime, LastWriteTime |
    Export-Csv -Path file-details.csv -NoTypeInformation -Encoding UTF8

The resulting CSV file can be opened in Microsoft Excel or another spreadsheet application.

The exported information includes:

  • Name: File name and extension.
  • FullName: Complete file path.
  • Length: File size in bytes.
  • CreationTime: File creation date and time.
  • LastWriteTime: Last modification date and time.

6. Useful PowerShell Parameters

The following parameters are particularly useful when working with files and folders.

Parameter Purpose
-File Returns files and excludes directories.
-Directory Returns directories only.
-Recurse Includes files and folders within subdirectories.
-Filter Filters results by a specified file pattern.
-Force Includes hidden and system items where accessible.
-Name Returns item names instead of full file information.
-LiteralPath Specifies an exact path without interpreting wildcard characters.
-Encoding Specifies the character encoding of the output file.

7. Important Considerations

Output File Location

If you specify only a filename, such as files.txt, PowerShell creates the output file in the current working directory. You can specify a complete destination path when you want to save it elsewhere.

Get-ChildItem -File |
    Select-Object -ExpandProperty Name |
    Out-File "D:\Reports\files.txt"

File Encoding

The -Encoding parameter controls how text is written to the output file. For example, -Encoding utf8 specifies UTF-8 encoding.

Encoding defaults differ between Windows PowerShell 5.1 and newer PowerShell versions. If a text file must be compatible with another application, specify the required encoding explicitly.

Hidden Files and Folders

To include hidden files where accessible, add the -Force parameter.

Get-ChildItem -File -Force -Recurse

Large Files and Folders

When processing a folder containing thousands of files or very large text files, the operation may take time and consume system resources. Use appropriate file filters and avoid unnecessarily processing unrelated files.

8. Frequently Asked Questions

Can PowerShell export all file names from a folder into a text file?

Yes. Use Get-ChildItem -File | Select-Object -ExpandProperty Name | Out-File files.txt to export the names of files in the current folder.

Can PowerShell include files from subfolders?

Yes. Add the -Recurse parameter to Get-ChildItem to include files from all subdirectories.

Can I combine hundreds of text files into one file?

Yes. PowerShell can process multiple text files using Get-ChildItem, ForEach-Object and Get-Content, then write the combined output using Out-File.

Does combining text files delete or modify the original files?

No. The commands described in this guide read the source files and write their contents to a separate output file. They do not delete or modify the original files.

Can PowerShell combine files from multiple folders?

Yes. Use the -Recurse parameter to search through subfolders, or specify multiple folder paths when appropriate.

9. Official Microsoft Documentation

For additional information about the PowerShell commands discussed in this article, refer to the official Microsoft documentation:

Conclusion

PowerShell provides a simple and effective way to manage files and folders without requiring third-party utilities. Whether you need to export a file listing, collect information from subfolders, or combine the contents of multiple text files, built-in cmdlets such as Get-ChildItem, Get-Content and Out-File can handle these tasks efficiently.

YOUR FEEDBACK

Was this guide useful?

Your answer helps us keep BISONKB accurate and practical.

Links are permitted only to published BISONKB articles.