1

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 1

2

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
  • How can you flow the arguments in? Hard coding is easy enough.
    – Ryan Leach
    Commented Jun 28, 2018 at 10:51
  • I assume like in any other batch script - see my edit. Commented Jun 28, 2018 at 11:25

You must log in to answer this question.

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