I'm looking for help in creating a script that can rename all of the files contained in a directory to a table referenced in a csv file. For example, I have a folder with random file names and I also have a csv file with the current file names in column A, and what the actual file name should be changed to in column B.
I thought about using something along the lines of:
get-childitem *pdf -force | foreach { rename-item $_ $_.Name.Replace((Import-Csv -path .\List.csv).filename, (Import-Csv -Path .\List.csv).newfilename) }
Note that filename & newfilename refer to column A and B respectively.
Nothing really happens when I run this script. I'm a novice with powershell so I've come to the limits of my knowledge of how to accomplish this task. Any and all help will be greatly appreciated!
Thank you all!
You can do the following if you are in the directory that contains the target files and the CSV file. This assumes you have headers in your CSV named
filenameandnewfilename.If the target directory is another location, you can simply modify the
Join-Pathstatements to use-Path.Note: If the CSV file does not contain headers, you will need to either utilize the
-Headerswitch inImport-Csvlike in Glenn's helpful answer or add the column headers to the CSV.