2

I am trying to add a couple simple PowerShell commands to a batch file. My preference is to keep the batch file self-contained vs calling an external .ps1 file.

Task:

Remove & char from filenames in a specific folder structure.

The following PowerShell commands work from the command line:

cd c:\Media\Downloads
Get-ChildItem -Filter '*&*' -Recurse | Rename-Item -NewName {$_.name -replace '&','testing' }"

Here's what I attempted to add to my Batch file, without success:

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "& cd c:\Media\Downloads"
 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "& Get-ChildItem -Filter '*&*' -Recurse | Rename-Item -NewName {$_.name -replace '&','testing' }"

Can anyone provide some advice, please?

1 Answer 1

4
powershell -c "Get-ChildItem 'c:\Media\Downloads' -Filter '*&*' -Recurse | Rename-Item -NewName {$_.name -replace '&','testing' }"
  • specify the path in Get-ChildItem
  • no need to specify the full path to powershell
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, @wOxxOm. Tried this line: powershell.exe -Command -c "Get-ChildItem 'c:\Media\Downloads' -Filter 'AA' -Recurse | Rename-Item -NewName {$_.name -replace 'AA','testing' }", but not seeing the replacement. Permissions issue?
@MarkPelletier Does "Get-ChildItem 'c:\Media\Downloads' -Filter 'AA' -Recurse return anything? Your filter has no wildcards
At command line, yes. I see results. From within Batch file, no result. (fixed the wildcard on my filter)
@MarkPelletier, -command -c is wrong, just leave one thing. The posted code in my answer works inside a batch file here. There might be something wrong in the other code.
My test batch file only has a single line (removed extra "-command"). I see the command window open, the PS line appears, but no result, no errors. Odd.
|

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.