1

I am trying to put 2 powershell commands into a .bat file so I can just click on the file and run them, without opening powershell, pasting in the commands, etc. The commands are:

cd Downloads
get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }

I currently have a .bat file with powershell -ExecutionPolicy Bypass -Command 'cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }', but it isn't working. I also tried:

powershell -Command 'cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }'
powershell -command 'cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }'
powershell -Command "cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }"
powershell -command "cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }"
powershell -ExecutionPolicy Bypass -Command "cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }"

How can I have a powershell command in a .bat file or something similliar?

3
  • 3
    why not put all your commands in a file.ps1 and then call it as powershell -file file.ps1? That way, you can edit the file.ps1 directly in Powershell ISE and have proper debugging support too, and you can even use enters instead of putting everything as a oneliner.
    – LPChip
    Commented Mar 18, 2022 at 20:45
  • also cd downloads can be done in the batch file itself. no need to put that to powershell, but if powershell is being executed from a completely different folder, it may not have Downloads as child and you may have to use something like: cd $env:userprofile\downloads
    – LPChip
    Commented Mar 18, 2022 at 20:47
  • Why are you not running PowerShell scripts with PowerShell Windows Terminal with the appropriate configured profile? Regardless if you run it from a command prompt a PowerShell session will till be launched
    – Ramhound
    Commented Mar 18, 2022 at 21:38

2 Answers 2

1

The problem is the sometimes weird windows function "CommandLineToArgvW" That is the function where all your parameters for the powershell exe will be pharsed with.

powershell -Command 'cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-", "") }'

will end up in

Param 000:  powershell
Param 001:  -Command
Param 002:  'cd
Param 003:  Downloads;
Param 004:  get-childitem
Param 005:  *.mp4
Param 006:  |
Param 007:  foreach
Param 008:  {
Param 009:  rename-item
Param 010:  $_
Param 011:  $_.Name.Replace(yt5s.com-,
Param 012:  )
Param 013:  }'

One correct escaping for your requested usecase is

powershell -Command "cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace(""yt5s.com-\",\"\")}"

This will result in:

Param 000:  powershell
Param 001:  -Command
Param 002:  cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace("yt5s.com-","")}

But i would stick to the following rule for such constructs.
Use " for the command string and inside the command string use '

Like this, it works too.

powershell -Command "cd Downloads; get-childitem *.mp4 | foreach { rename-item $_ $_.Name.Replace('yt5s.com-','')}"

I have build and published a application (Quick and dirty) for you to test the behavior
In the repository take a look at bin debug if you are not able to compile the code.
It is a .net 2.0 exe so it should run nearly on every windows pc.

Screenshot

Link to repository CommandLineHelper

0

You can try in cmd or bat file:


>powershell -nop -c "gci $HOME\Downloads\*yt5s.com-*.mp4 | ? {ren $_ $_.Name.Replace('yt5s.com-','')}"

# or 

>powershell -nop -c "gci $HOME\Downloads\*yt5s.com-*.mp4 | ? {ren $_ -N $_.Name.Replace('yt5s.com-','')}"

# or 

>powershell -nop -c "Get-ChildItem  $HOME\Downloads\*yt5s.com-*.mp4 | foreach { rename-item $_ -New $_.Name.Replace('yt5s.com-','')}"

# or 

>powershell -nop -c "Get-ChildItem $HOME\Downloads\*yt5s.com-*.mp4 | foreach { rename-item $_ -NewName $_.Name.Replace('yt5s.com-','')}"

Maybe you know the -Whatif | -wi option, but I'll suggest it for those who don't:

  • Use -Whatif | -wi to test your command without taking any action, just indicating the changes that would be made
>powershell -nop -c "gci $HOME\Downloads\*yt5s.com-*.mp4 | ? { ren $_ $_.Name.Replace('yt5s.com-','') -wi }"

# Or

>powershell -nop -c "Get-ChildItem $HOME\Downloads\*yt5s.com-*.mp4 | foreach { rename-item $_ -NewName $_.Name.Replace('yt5s.com-','') -Whatif }"

# results: 

What if: Performing the operation "Rename File" on target "Item: C:\Users\ecker\Downloads\Name Test yt5s.com- 0001.mp4 Destination: C:\Users\ecker\Downloads\Name Test  0001.mp4".
What if: Performing the operation "Rename File" on target "Item: C:\Users\ecker\Downloads\Name yt5s.com- Test 0002.mp4 Destination: C:\Users\ecker\Downloads\Name  Test 0002.mp4".
What if: Performing the operation "Rename File" on target "Item: C:\Users\ecker\Downloads\yt5s.com-Name- Test_0004.mp4 Destination: C:\Users\ecker\Downloads\Name- Test_0004.mp4".

  • In cmd prompt:
>for %i in ("%homepath%\Downloads\*yt5s.com-*.mp4")do set "_file=%~nxi" & cmd.exe /v/e/c ren "%~fi" "!_file:yt5s.com-=!"
  • In cmd/bat file:
@echo off 

for %%i in ("%homepath%\Downloads\*yt5s.com-*.mp4"
    )do set "_file=%%~nxi" & cmd.exe /v/e/c ren "%%~fi" "!_file:yt5s.com-=!"



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .