How to Change File Extensions with PowerShell

To change a file extension in PowerShell, rename the file with Rename-Item and give it the new name, extension included:

Rename-Item -Path .\Reports\austin-sales.txt -NewName austin-sales.csv
Get-ChildItem -Path .\Reports -Filter austin* -Name

Output:

austin-sales.csv

In this tutorial, I’ll show you how to change file extensions with PowerShell for one file, a whole folder and its subfolders, plus two traps I hit while testing in PowerShell 7 and 5.1.

The sample Reports folder we’ll use

Every example starts from this folder. It has mixed-case extensions, a file with no extension and one subfolder:

Get-ChildItem -Path .\Reports -Recurse -File -Name

Output:

austin-sales.txt
denver-sales.TXT
index.htm
menu.html
README
seattle_txt_notes.txt
Archive\boston-sales.txt

The -Name switch makes Get-ChildItem in PowerShell return relative paths only, which keeps the list short.

Method 1: Change the extension of one file

Pass the old path to -Path and the full new name to -NewName. Add -PassThru if you want the renamed file back:

$file = Rename-Item -Path .\Reports\austin-sales.txt -NewName austin-sales.csv -PassThru
$file.Name

Output:

austin-sales.csv

How does this code work? Rename-Item changes only the name, so the content stays the same. -NewName takes a name, not a path. The PowerShell Rename-Item guide covers the other parameters.

Pro Tip: changing .txt to .csv doesn’t convert anything. It only tells Windows which app should open the file.

Method 2: Change the extension of all files in a folder

NewName doesn’t accept wildcards. Instead, pipe the files into Rename-Item and build each new name in a script block:

Get-ChildItem -Path .\Reports -Filter *.txt |
    Rename-Item -NewName { $_.Name -replace '\.txt$', '.log' } -PassThru |
    Select-Object -ExpandProperty Name

Output:

austin-sales.log
denver-sales.log
seattle_txt_notes.log
PowerShell 7 Get-ChildItem piped to Rename-Item changing every .txt file extension to .log
All three .txt files now end in .log, including the uppercase .TXT (PowerShell 7)

How does this code work? Get-ChildItem -Filter picks the .txt files, and $_ is the current file inside the script block. The -replace operator ignores case, so denver-sales.TXT is renamed too.

Pro Tip: run it with -WhatIf first. Each file prints a What if: line with the old and new name, and nothing changes.

Why escape the dot and add $?

Microsoft Learn’s example uses -replace '.txt', '.log'. In a regex, the dot matches any character, so this pattern also hits _txt in the middle of a name:

Get-ChildItem -Path .\Reports -Filter *.txt |
    Rename-Item -NewName { $_.Name -replace '.txt', '.log' } -PassThru |
    Select-Object -ExpandProperty Name

Output:

austin-sales.log
denver-sales.log
seattle.log_notes.log

Seattle’s file became seattle.log_notes.log. The \. matches a real dot, and $ anchors the match to the end. More patterns are in PowerShell regex examples.

Method 3: Use [System.IO.Path]::ChangeExtension

If regex isn’t your thing, .NET has a method that swaps only the last extension. It works the same in PowerShell 7 and 5.1:

Get-ChildItem -Path .\Reports -Filter *.txt |
    Rename-Item -NewName { [System.IO.Path]::ChangeExtension($_.Name, 'md') } -PassThru |
    Select-Object -ExpandProperty Name

Output:

austin-sales.md
denver-sales.md
seattle_txt_notes.md

How does this code work? ChangeExtension adds the leading period when you leave it out. $_.BaseName + '.md' gives the same result. See how to get a file name without its extension for BaseName.

Method 4: Change file extensions in subfolders too

Add -Recurse to include every subfolder. Each file stays in its own folder:

Get-ChildItem -Path .\Reports -Filter *.txt -Recurse |
    Rename-Item -NewName { $_.BaseName + '.csv' } -PassThru |
    Resolve-Path -Relative

Output:

.\Reports\austin-sales.csv
.\Reports\denver-sales.csv
.\Reports\seattle_txt_notes.csv
.\Reports\Archive\boston-sales.csv
PowerShell 7 Rename-Item changing file extensions in subfolders with Get-ChildItem -Recurse
boston-sales.txt in the Archive subfolder was renamed as well (PowerShell 7)

How does this code work? Resolve-Path -Relative just prints shorter paths. To check what a recursive search will catch first, see how to find all files with a specific extension.

Method 5: Add or remove a file extension

Files with no extension have an empty Extension property. Filter them with Where-Object and append one:

Get-ChildItem -Path .\Reports -File |
    Where-Object Extension -eq '' |
    Rename-Item -NewName { $_.Name + '.md' } -PassThru |
    Select-Object -ExpandProperty Name

Output:

README.md

To strip an extension, ChangeExtension with a null value should do it. But PowerShell