Run a powershell script exactly like a batch command

How can I get the effect of running a powershell script from a batch command.

e.g. I have a few batch commands on PATH specific to my user, I use these to make using the command line & git easier for my job.

However I havn't gotten into the habit of learning PowerShell yet, so don't like using PowerShell if I can help it.

However as stuff migrates, more and more scripts appear to be powershell scripts.

How can I create a 'shortcut' batch file, that forwards all arguments passed to it, to a powershell script of the same name?

E.g. I have a Powershell script called stree.ps1

Start-Process "C:\Users\ryan.leach\AppData\Local\SourceTree\SourceTree.exe" -ArgumentList "-f $((Resolve-Path $args[0]).toString())"

that will launch SourceTree with the argument corresponding to a passed in path.

How can I create a stree.bat that calls stree.ps1 forwarding all arguments to stree.ps1, without having to update stree.bat if small changes are made to stree.ps1?

1 Answer

If you run stree.ps1 in PowerShell like this:

stree some_argument

then your stree.bat should look like:

PowerShell -Command "stree some_argument"

See the documentation for more details.

Edit: I haven't tested it, but I assume you would pass the arguments in the same way as with any other batch script:

PowerShell -Command "stree %*"

and then run:

stree.bat some_argument some_other_argument

Edit 2: Ok, I tested it and it works like expected.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like