1

I'm running a batch file within PowerShell. It calls up another program to do a bunch of maps which it outputs to a subfolder called "GRD"

I then want to rename the files in the GRD folder by adding spaces " " where there are underscores "_", so I'm using the PoweShell command:

DIR | Rename-Item -NewName { $.name -replace ""," " }

This line works fine if I type it into the PS command window after I've run my batch file. But is there a way that I could incorporate this line into the end of my batch file? I've tried:

cd GRD

DIR | Rename-Item -NewName { $.name -replace ""," " }

But i get the error:

C:\Data\GRD>DIR | Rename-Item -NewName { $.name -replace ""," " }

'Rename-Item' is not recognized as an internal or external command, operable program or batch file.

  • Why are you trying to make a batch file, and not a powershell script? (ps1) – Zoredache Aug 11 '14 at 19:59
  • @Zoredache: originally a batch file was sufficient for my task of generating maps. With the output file names not being exactly right, I read that using PowerShell could do the name change. Pls keep in mind I'm not an expert and just learning my way through. – Sols Aug 11 '14 at 20:22
1

Aside from the fact that your example doesn't work properly at all, in general to use PowerShell form a batch file you specify that you want to run PowerShell, and feed the PS command-line to it as an argument.

So in your batch use something like:

powershell "DIR | Rename-Item -NewName { $.name -replace \"\",\" \" }"

Note the \ in front of the double-quotes to escape them inside the argument delimiting double-quotes.

  • does not seem to work. I get the error: 'powershell' is not recognized as an internal or external command, operable program, or batch file. – Sols Aug 11 '14 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.