0

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?

2 Answers 2

1

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

dir *.mxd | copy-item -destination { 'prefix_' + $_.name } -whatif
Sign up to request clarification or add additional context in comments.

2 Comments

Almost, though your answer would have helped had I seen it before finally finding my own answer. I need to copy and rename the files, hence copy-item instead of rename-item.
Just change rename-item -newname to copy-item -destination.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.