Windows 10, Windows 11, Windows Server

How to Export a List of Files in a Folder to a .TXT File Using PowerShell



🧐 Why Export a Folder’s File List to a Text File?

Recently, I needed to generate a list of all the files contained within a folder on my Windows machine and export that list to a text file. At first, I considered using third-party software, but then I realized I could easily accomplish this using Windows PowerShell. To save time, I asked ChatGPT to create a script for me. After testing it and fixing a few minor bugs, I ended up with a simple, reliable solution that does exactly what I need.

This is just a quick post for anyone looking for an easy way to export folder file lists using PowerShell—no extra software required.

📄 Export Your Folder’s File List to a TXT File | PS Script

💡 Here’s a simple PowerShell script you can use to generate a list of all files in a folder and export it to a .txt file.

PowerShell
# Prompt the user to select a folder
$Folder = (New-Object -ComObject Shell.Application).BrowseForFolder(0, "Select a folder", 0).Self.Path

if ($Folder) {
    # Get file names in the selected folder (non-recursive)
    $FileNames = Get-ChildItem -Path $Folder -File | Select-Object -ExpandProperty Name

    # Define output file path (in same folder)
    $OutputFile = Join-Path $Folder "FileList.txt"

    # Write file names to output file
    $FileNames | Out-File -FilePath $OutputFile -Encoding UTF8

    Write-Host "File list saved to: $OutputFile"
} else {
    Write-Host "No folder selected. Exiting script."
}

📝 This script prompts you to choose a folder, collects the names of all files (excluding subfolders), and saves them to a FileList.txt file in the same directory. It’s a quick and easy way to generate a flat file listing without recursion.

🚀 How to Use the PowerShell Script to Export Your File List

Step 1: Open PowerShell

  • Click the Start menu, type PowerShell, then click Windows PowerShell to open the standard PowerShell console.
  • Note: You can also use PowerShell ISE (a built-in script editor) if you prefer, but for this simple script, the standard console is easier and faster.

Step 2: Copy the Script

  • Select and copy the entire script provided.

Step 3: Paste the Script into PowerShell

  • Right-click anywhere inside the PowerShell window to paste the script.

Step 4: Run the Script

  • Press Enter to execute it.

Step 5: Select Your Folder

  • A window will pop up asking you to choose a folder. Navigate to the folder you want to list the files of, then click OK.

Step 6: Check the Output

  • The script will create a file named FileList.txt inside the folder you selected. Open it with any text editor (like Notepad) to see your list of files.

✅ Conclusion

And that’s it! With just a few lines of PowerShell, you’ve created a simple way to export a folder’s file list to a .txt file. This can be especially handy for documentation, audits, or organizing your files more efficiently.

🌿 Final Thoughts

PowerShell is a powerful tool that can simplify everyday tasks like organizing and documenting files. Whether you’re managing projects, backing up data, or just staying organized, exporting file lists can save time and reduce manual effort. Explore further customizations to make the script fit your exact needs!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *