PowerShell Script: Bulk Rename of Files

I ran across a question on StackoverFlow about renaming a list of files in a directory. The goal for me was to create a script that would allow me to use less administrative work and get the job done quickly.

**Create global variables that can be used for all the functions.**

    Function Variables{
    $Global:CreateListFile="F:\RemoveFiles\"
    $Global:ListofFilesCSV="F:\FileList.csv"
    $Global:ListofFilesCSV_NewName="F:\FileList2.csv"
    $Global:RenameCSV="F:\Rename.csv"
    }

**Create a list of Files/Directories that we want to rename. We then let PowerShell open up Excel and allow you to edit the CSV. Once closed – the script will start again.**

    Function CreateList{
    # Create List File using .Csv
    Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
    #Get-Content $Global:ListofFilesCSV
    }

**Now with the edited list we can setup what we want to rename the structure.**

    Function CreateRenameList{
    $File = Import-CSV $Global:ListofFilesCSV
    $File2 = Import-CSV $Global:ListofFilesCSV
    $i = 0
    $File | ForEach `
    {
      $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
      $i++
    }
    $File | Export-CSV $Global:RenameCSV -notype
    #Get-Content $Global:RenameCSV
    (Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
    }

**Now with the Rename list completed we can now rename the files we wanted.**

    Function Rename{
    Import-Csv $Global:RenameCSV | 
    ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
    }

**Call the functions**

    Function GOGO{
    Variables
    CreateList
    CreateRenameList
    Rename
    }
    GOGO

I hope someone finds this helpful!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s