PowerShell Script to Copy File Names to Matching Files in Another Folder

At times in one's digital life, new files are acquired to replace existing ones on a 1:1 basis. I'll leave it to your imagination to think of specific situations, but I have a recurring one, and my normal tool (Bulk Rename Utility) didn't have a quick solution. I looked around online, but couldn't find anything I liked enough to steal, so I made my own PowerShell script. It's nothing complex or ground-breaking, but for others with the same need, it should be pretty easy to grab and use.

As with any script you didn't write, do not use this in any consequential situation unless you have read through it, fully understand what it's doing, and have performed the appropriate backup tasks.
 

Requirements

Access Requirements:
-Write access to destination directory
-Read access to source directory
Script Language: PowerShell
Version: 5.1
Input File? No
Other Requirements:
-PowerShell execution policy that allows unsigned local scripts
-The specified folders must contain the same number of files
   

Walkthrough

  1. Call the script. The easiest ways to do this are:

    1. Copy the script directly into PowerShell ISE and run. 
      1. This is probably best for a one-off situation.
      2. There can be window focus issues with the folder selection dialog boxes. If it appears to be stuck, minimize all your windows.

    2. Copy the script into a .ps1 file in a known directory and call it from a PowerShell prompt.
      1. This is probably best if you're planning on using it multiple times.
      2. There are no window focus issues with this method.

  2. You will be prompted to choose the source folder. This is the folder that contains the "old" files with the names you want to copy. Navigate to the folder, select it, then click OK.

  3. You will be prompted to choose the destination folder. This is the folder that contains the "new" files that will be renamed using the info from files in the source folder. Navigate to the folder, select it, then click OK.

  4. Before the actual modifications start, the script will do a check to make sure the two folders have the same number of files in them.

    1. This is a safety check to make it unlikely anything will happen if you accidentally select the wrong folder.

  5. The script will do the renames.

  6. You will be presented with a dialog box that contains a summary of the tasks done.
     

Script

# Import resources for dialog box creation
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName PresentationCore,PresentationFramework

# Input source folder
$FolderBrowserSource = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    RootFolder = "MyComputer"
    ShowNewFolderButton = $false
    Description = "Select the source folder"
}

[void]$FolderBrowserSource.ShowDialog()

# Input destination folder
$FolderBrowserDest = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    RootFolder = "MyComputer"
    ShowNewFolderButton = $false
    Description = "Select the destination folder"
}

[void]$FolderBrowserDest.ShowDialog()

# Retrieve number of files
$NumFilesSource = (Get-ChildItem -Path $FolderBrowserSource.SelectedPath | Measure-Object).Count
$NumFilesDest = (Get-ChildItem -Path $FolderBrowserDest.SelectedPath | Measure-Object).Count

# Test number of files in source and destination folders, exit and error if mismatch.
If ($NumFilesSource -eq $NumFilesDest) {

# Load folders into arrays and set rename counter to 0
$SourceItems = Get-ChildItem -Path $FolderBrowserSource.SelectedPath
$DestItems = Get-ChildItem -Path $FolderBrowserDest.SelectedPath
$FileCounter = 0
    
    # Loop through all source folder files
    ForEach ($SourceFile in $SourceItems) {

    # Rename file
    Rename-Item $DestItems[$FileCounter].FullName -NewName $SourceFile.Name

    # Increment counter
    $FileCounter++

    }

# Prompt with summary of actions
$SourcePath = $FolderBrowserSource.SelectedPath
$DestPath = $FolderBrowserDest.SelectedPath
[System.Windows.MessageBox]::Show("Number of files renamed: $FileCounter`n`nSource Folder`n$SourcePath`n`nDestination Folder`n$DestPath")

}

Else {

[System.Windows.MessageBox]::Show("Number of files in the source folder does not match number of files in the destination folder.")

}

Comments