Yes. Read the CSV file, I assume that field 1 is the old name and field 2 is the new name, and then enumerate the lines using Move-Item to move source to destination. Alternatively first test with -WhatIf or Copy-Item
.
Test 1, Test1
Test 2, Test2
Test 3, Test3
Note that I'm assuming you're running a new enough version of PS (at least 5.1+) such that Import-Csv is available to easily parse the CSV. If for some reason you cannot do that then use Get-Content
to load the file contents and then manually split the line instead.
# Import the CSV file, assuming no header in the file so create one manually
$mappings = Import-Csv $mapFile -Header 'OriginalName', 'NewName'
# Copy each source folder into the destination folder by combining the names with the source and target paths
$mappings | foreach-object { Copy-Item -Path ([System.IO.Path]::Combine($sourcePath, $_.OriginalName)) -Destination ([System.IO.Path]::Combine($targetPath, $_.NewName)) -Recurse }
Here I'm using the Copy-Item
for several reasons.
-
Move-Item
is documented as not working if you are moving across drives. Given your post it sounds like this is true so Move-Item
won't work.
-
Move-Item
does not allow rename while moving. You would therefore need to first move the items then use Rename-Item
to rename each folder.
- For testing/recovery purposes if something goes wrong you can blow away the target structure and run your command again. Only after you've verified everything copied correctly (perhaps by counting files) should you then delete the source.
Move-Item
would not allow this. If something went wrong you'd have to move all the directories back and try again, potentially.