Created
November 20, 2021 10:04
-
-
Save tackme31/a402947b7b571ebe5b865faef7d18504 to your computer and use it in GitHub Desktop.
PowerShell script to rename extension of files in a specified directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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