Daniel Padia
Daniel Padia

Reputation: 15

Get-ChildItem piped to Copy-Item with rename to same directory

I am trying to bulk copy/rename files of a certain extension in a specific folder to the same folder with a prefix. I am certain this is a problem with my own Powershell inexperience. I simply do not know how to set up the command.

I am trying to take all the files of extension .mxd within a folder and rename them from something.mxd to _prefix_something.mxd_

I am pretty sure the first section is fine: Get-ChildItem *.mxd But I don't know how to work with Copy-Item well enough. My understanding is that -Destination is where you would put the new file name, so how do I input ("current-folder\prefix" + $_.Name + ".mxd") ??

I am sure I am just missing a few key points to get this to work. All I am getting is a single "prefix_.mxd" file. It is the same size as the last file in the Get-ChildItem list. But if I bring the foreach loop back in, it asks for path values. What am I missing?

Upvotes: 0

Views: 945

Answers (2)

js2010
js2010

Reputation: 27626

Like this? The doc for rename-item has a similar example.

dir *.mxd | copy-item -destination { 'prefix_' + $_.name } -whatif

Upvotes: 1

Daniel Padia
Daniel Padia

Reputation: 15

Stupid. I have to force scriptblock with curlybraces {} instead of using parenthesis (), and I can skip the path to the current folder by starting with the prefix.

Upvotes: 0

Related Questions