How To Run A Batch File In Powershell

How To Run A Batch File In Powershell 4,0/5 6130 reviews

I need help to run a powershell script in a batch file. I run the powershell script manually every single day and I wanted to automate it if possible with your help. Note that I am renaming a large number of files. This is the script I ran under the command prompt of powershell. To run or convert batch files to PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, for example, deletefolders.ps1. Input the following into the script. Scenario 2: Calling powershell script file from batch. Create sample powershell file (powershell-to-create-directories.ps1)create 2 example1 and example2 folders like we did in earlier example. Here we will do in opposite manner. Note: you might need permissions to execute powershell script on your machine.

I am working on a batch file that can run a PowerShell script when the batch file is double clicked. I am having difficulty getting the PowerShell script to do anything when activated by the batch file. The PowerShell script works perfectly when I test out the ps1 file. But once I call from it the batch file it doesn't seem to work.

Batch file:

Worthwelle
3,0684 gold badges14 silver badges27 bronze badges
Olujide OluadeOlujide Oluade

How To Run A Batch File In Power Shell

1 Answer

It is an escaping issue,

  • cmd will interpret the inner double quotes as ending the first one
    as the single quotes have a special meaning only to PowerShell.
  • use a backslash to escape one ' double quote
LotPingsLotPingsRunHow To Run A Batch File In PowershellPowershell
5,6851 gold badge9 silver badges24 bronze badges

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

I am newer to making batch files. I am trying to run a command that will open windows PowerShell in a specific directory, and then once there to run a command.

So far I have the following. Powershell.exe -noexit -command 'cd 'c:DevProductDev' This seems to work in opening PowerShell to the right location, but I cannot get it to then run the command node sp.

I have tried the following with no success PowerShell -NoExit -Command 'Write-Host 'node sp'

Also, is it possible to have it open Windows PowerShell in the blue display screen, rather than in the CMD (black/white) window?

AustinAustin

1 Answer

Invoke Batch File Powershell

As you have not provided your complete bat-file I guess that it is

This is the wrong approach. You are first starting a Powershell which changes to the given directory and then stays open. If you exit this powershell the bat file will start the second PowerShell.

You need to run one PowerShell and let it execute both commands. One way is to

Another way is to write a PowerShell script file like MyScript.ps1 with this content:

and start PowerShell not giving it the command to execute but the script to execute (see also this and this StackOverflow question). You could either run this command directly or put it in a bat file, or even use it as the command to execute in a lnk file:

A problem with PowerShell is that per default it does not run scripts that are not (cryptographically) signed. The easy way to solve that is the -ExecutionPolicy Bypass parameter.

Community

How To Run A Batch File In Powershell

Werner HenzeWerner Henze
4,0493 gold badges14 silver badges30 bronze badges

Not the answer you're looking for? Browse other questions tagged windowscommand-linewindows-8.1batchpowershell or ask your own question.