PowerShell: Export a File List to CSV for Excel Without Garbled Text

Advertisement
File Operations
Advertisement

When you clean up a shared drive or check a delivery, it helps to have every file in a folder in one Excel sheet: which subfolder it sits in, how big it is, and when it last changed. Clicking through File Explorer folder by folder does not scale.

The short answer: collect the files with Get-ChildItem and write them out with Export-Csv. The part that decides whether Excel shows the names correctly is the encoding: use -Encoding UTF8 in Windows PowerShell 5.1 and -Encoding utf8BOM in PowerShell 7. The script below picks the right one for you, so you only need to change the folder path.

The exported file list opened in Excel with every name displayed correctly
The CSV opened in Excel 2013. Accented letters and the emoji came through intact

Why use PowerShell for a file list?

There are a few ways to get a list of files out of Windows. If you want to sort or filter the result in Excel, a CSV with separate columns is the most useful, and PowerShell gives you that.

Method What you get Drawback
dir /s in Command Prompt A plain text listing Sizes and dates are not in columns, so Excel cannot sort them
Copy as path in File Explorer Paths of the files you selected Does not look inside subfolders, with no size or date
PowerShell (this post) A CSV with subfolders, sizes, plus modified dates as columns The text gets garbled in Excel unless you set the encoding

A CSV file is just text separated by commas. It does not record which character encoding it uses. Excel checks the first three bytes for a BOM (byte order mark). If the BOM is there, Excel reads the file as UTF-8. If it is not, Excel falls back to the legacy code page of your Windows language setting. Garbled names in Excel come from that mismatch.

The script

Open PowerShell (5.1 or 7 both work), edit the first two lines, then paste the whole script. You do not need to run it as administrator. When it finishes, the CSV opens in Excel.

# ===== Edit these two lines =====
$folder = "C:\work\Projects"   # the folder to list
$csv    = Join-Path ([Environment]::GetFolderPath("Desktop")) "file-list.csv"   # where to save the CSV (your Desktop here)
# ================================

$opt  = if ($PSVersionTable.PSVersion.Major -ge 6) { @{ Encoding = "utf8BOM" } } else { @{ Encoding = "UTF8"; NoTypeInformation = $true } }
$root = (Resolve-Path -LiteralPath $folder).ProviderPath.TrimEnd("\")
$list = Get-ChildItem -LiteralPath $root -Recurse -File -ErrorAction SilentlyContinue -ErrorVariable errs |
    ForEach-Object {
        $sub = $_.DirectoryName.Remove(0, $root.Length).TrimStart("\")
        [PSCustomObject]@{
            Folder    = if ($sub) { $sub } else { "(top)" }
            Name      = $_.Name
            Extension = $_.Extension
            SizeKB    = [math]::Round($_.Length / 1KB, 1)
            Modified  = Get-Date $_.LastWriteTime -Format "yyyy-MM-dd HH:mm"
            FullPath  = $_.FullName
        }
    }
$list | Sort-Object Folder, Name | Export-Csv -LiteralPath $csv @opt
"Exported {0} files: {1}" -f @($list).Count, $csv
if ($errs) { "Skipped {0} location(s) that could not be read (for example, no permission)" -f $errs.Count }
Invoke-Item -LiteralPath $csv

Use and modify the script as you like.

What to change

Line What it is Example
$folder (line 2) The folder to list. A network share (\\server\share) works too "D:\Deliverables"
$csv (line 3) Where to save the CSV. By default it creates file-list.csv on your Desktop "D:\work\list.csv"

The Desktop path comes from [Environment]::GetFolderPath("Desktop") because on PCs where OneDrive backs up the Desktop, the real Desktop folder lives inside OneDrive, not in C:\Users\name\Desktop.

Sample output

I ran it on a test folder with two subfolders and six files. The console showed:

Exported 6 files: D:\test\file-list.csv

Here is the CSV it wrote (paths shortened):

"Folder","Name","Extension","SizeKB","Modified","FullPath"
"(top)","Budget 2026.xlsx",".xlsx","20","2026-11-20 09:30","D:\test\Projects\Budget 2026.xlsx"
"(top)","readme.txt",".txt","0","2026-11-21 10:30","D:\test\Projects\readme.txt"
"Invoices","Café invoice.pdf",".pdf","2.9","2026-11-20 14:30","D:\test\Projects\Invoices\Café invoice.pdf"
"Invoices","Müller GmbH.docx",".docx","0.5","2026-11-20 19:30","D:\test\Projects\Invoices\Müller GmbH.docx"
"Photos\2026","Beach 🏖.jpg",".jpg","146.5","2026-11-21 00:30","D:\test\Projects\Photos\2026\Beach 🏖.jpg"
"Photos\2026","IMG_0001.jpg",".jpg","2148.4","2026-11-21 05:30","D:\test\Projects\Photos\2026\IMG_0001.jpg"
Column Contents
Folder The subfolder, relative to the folder you listed. Files at the top level show "(top)"
Name and Extension As is. Filtering on Extension in Excel is handy
SizeKB Size in KB with one decimal place. It is a number, so Excel can sort it and add it up
Modified Last modified time as 2026-11-21 10:30, which Excel treats as a date
FullPath Where the file is. Paste it into File Explorer to open it

If some folders cannot be read (no permission), the script keeps going and reports how many it skipped at the end. I tested this with a folder I had denied myself access to.

How each -Encoding value behaves in Excel

Line 6 (the $opt line) is the important part. Windows PowerShell 5.1 and PowerShell 7 use different defaults for Export-Csv, and even the same -Encoding value means different things. I exported the same list (names with Japanese text, "é" and an emoji) with each setting, then opened every file in Excel 2013 on a Japanese-language Windows 11 PC.

Version -Encoding What gets written Result in Excel
5.1 (none) ASCII Every non-ASCII character becomes "?" in the file itself, so it cannot be recovered
5.1 Default The legacy code page (Shift-JIS on my PC) é became e; the emoji became ??
5.1 UTF8 UTF-8 with BOM Everything displays correctly
7 (none), UTF8, Default UTF-8 without BOM Garbled (on my PC, Café showed up as Cafテゥ)
7 utf8BOM UTF-8 with BOM Everything displays correctly
A CSV exported by PowerShell 7 without an Encoding parameter, opened in Excel, with garbled names
PowerShell 7 with no -Encoding. There is no BOM, so Excel guesses the wrong encoding

The catch is that -Encoding UTF8 writes a BOM in 5.1 but not in 7. The common advice to "add -Encoding UTF8" comes from the 5.1 days and stops working once you move to PowerShell 7. On the other hand, utf8BOM only exists in 7, and 5.1 rejects it with an error. That is why the script checks $PSVersionTable.PSVersion.Major and switches the setting. -Encoding Unicode (UTF-16) kept the characters, yet Excel put each whole line into column A instead of splitting on commas, so UTF-8 with BOM is the safer choice for Excel.

To see which PowerShell you are using, run the line below. 5.x means Windows PowerShell 5.1, while 7.x means PowerShell 7.

$PSVersionTable.PSVersion

Things that tripped me up

Network shares put every file under "(top)"

My first version read the folder path with (Resolve-Path $folder).Path. It worked on drive letters, but when I pointed it at a NAS share, it printed a red "Exception calling Remove" error for every file, and the Folder column said "(top)" for all 54 files. The count at the end still looked normal, which made it easy to miss.

For a UNC path, .Path comes back as Microsoft.PowerShell.Core\FileSystem::\\server\share, with a provider prefix in front. .ProviderPath returns the plain \\server\share. The script now uses .ProviderPath, and I confirmed the subfolders come out correctly on a share in both 5.1 and 7.

-NoTypeInformation warns in PowerShell 7

In 5.1, Export-Csv adds a "#TYPE System.Management.Automation.PSCustomObject" line at the top unless you pass -NoTypeInformation. In PowerShell 7.7, passing it prints a warning that the parameter is obsolete, because 7 never writes that line. The script only adds it in 5.1, so neither version complains.

Legacy code pages silently replace characters

If an older system needs the legacy code page (Default in 5.1, ansi in 7), characters that do not exist in it are swapped out without any error. In my test on a Japanese-language PC, Café résumé became Cafe resume, while the emoji became ??. On a Western-language PC, "é" fits in the code page, but an emoji or other characters outside it get replaced the same way. For a file inventory that means wrong names, so stick to UTF-8 with BOM unless you really need the old encoding.

Common tweaks

Replace the matching line in the script:

# Include hidden files too (add -Force)
$list = Get-ChildItem -LiteralPath $root -Recurse -File -Force -ErrorAction SilentlyContinue -ErrorVariable errs |

# Only Excel workbooks (add -Filter)
$list = Get-ChildItem -LiteralPath $root -Recurse -File -Filter *.xlsx -ErrorAction SilentlyContinue -ErrorVariable errs |

# Largest files first (change the last sort)
$list | Sort-Object SizeKB -Descending | Export-Csv -LiteralPath $csv @opt

Once you have the list, the next step might be a bulk rename with a one-line undo. To confirm that files have not changed, see Get-FileHash: export a folder's hashes to CSV.

Summary

  • Get-ChildItem -Recurse -File plus Export-Csv gives you an Excel-ready list that includes subfolders
  • Excel needs UTF-8 with BOM: -Encoding UTF8 in 5.1, -Encoding utf8BOM in 7. In 7, plain UTF8 has no BOM and comes out garbled

References

Comments

Copied title and URL