Skip to content

Instantly share code, notes, and snippets.

@tackme31
Created November 20, 2021 10:04
Show Gist options
  • Save tackme31/a402947b7b571ebe5b865faef7d18504 to your computer and use it in GitHub Desktop.
Save tackme31/a402947b7b571ebe5b865faef7d18504 to your computer and use it in GitHub Desktop.
PowerShell script to rename extension of files in a specified directory.
<#
.SYNOPSIS
PowerShell script to rename extension of files in a specified directory.
.EXAMPLE
PS> Rename-Extension -Path . -ExtensionFrom txt -ExtensionTo md -Recurse
.PARAMETER Path
Folder that contains the file to be renamed
.PARAMETER ExtensionFrom
Extension name of the conversion source.
.PARAMETER ExtensionTo
Extension name of the conversion destination.
.PARAMETER Recurse
If enabled, recursively change the extension.
#>
param (
[Parameter(Mandatory=$true)]
[string]
$Path,
[Parameter(Mandatory=$true)]
[string]
$ExtensionFrom,
[Parameter(Mandatory=$true)]
[string]
$ExtensionTo,
[Parameter(Mandatory=$false)]
[switch]
$Recurse
)
Get-ChildItem $Path -Filter "*.$ExtensionFrom" -Recurse:$Recurse | ForEach-Object {
$fullname = $_.FullName
$newname = $_.Name -replace "$ExtensionFrom$",$ExtensionTo
Rename-Item -LiteralPath $fullname -NewName $newname
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment