I am using the following command to copy files from a network share to my local hard drive based on a CSV file.
import-csv C:\TEST\test.csv | foreach {copy-item -path $_.npath -destination 'C:\TEST\'}
The next step would be to then use a different CSV file to rename these files based on their current file name.
Is there a command that will allow me to copy and item and rename it as well?
If you have a CSV containing two columns, oldfilepath and newfilename, then you can use the following.
Notice how the
$_.newfilenameis encapsulated inside a$(). That’s because$_.newfilenameis an expression (since we are getting a property out of the variable), and not a variable.$()tells PowerShell to solve the expression before using it in the string. If we don’t use it, it would have used the whole csv-object for that row($_) as a string and returned an error.