PS: Bulk Renaming Extensions

less than 1 minute read

Description:

This simple one liner script (line 34 or 39) helps me change extensions from one to another for a folder or recursively.

To Resolve:

  1. Script:
[Cmdletbinding()]
    param
    (
        [Parameter(Position = 0, Mandatory = $true)]
        [String]$Source,
        
        [Parameter(Position = 1, Mandatory = $true)]
        [String]$OldExtension,
        
        [Parameter(Position = 2, Mandatory = $true)]
        [String]$NewExtension,
        
        [Switch]$Recurse,
        
        [string]$logfile = "$PSScriptRoot\..\Logs\Rename-Items.log"
    )

    Begin
    {
        
        [String]$OldExtension = "." + $OldExtension
        [String]$NewExtension = "." + $NewExtension


    }
    
     Process
    {
        
        
                
        If ([Bool]($MyInvocation.BoundParameters.Keys -match 'Recurse'))
        {
            Get-Childitem $Source -Filter ("*" + $OldExtension) -Recurse | Rename-Item -Newname { [Io.Path]::Changeextension($_.Name, $NewExtension) }
            Log "Renamed all items in $Source and subfolders from $OldExtension to $NewExtension" 
        }
        Else
        {
            Get-Childitem $Source -Filter ( "*" + $OldExtension ) | Rename-Item -Newname { [Io.Path]::Changeextension($_.Name, $NewExtension) }
            Log "Renamed all items in $Source from $OldExtension to $NewExtension" 
        }
    }

 

This can be found in my gwFileSystem section.