Make it easier to wrap powershell scripts in batch files - by VadimC

Status : 

 


3
1
Sign in
to vote
ID 724524 Comments
Status Active Workarounds
Type Suggestion Repros 0
Opened 2/11/2012 1:18:22 PM
Access Restriction Public

Description

If I want to make a powershell script as easy to use as old DOS batch files, I have to create a companion .cmd/.bat file, which just launches the real script.  This is inconvenient, as both file have to be transported together, etc, etc.

Okay, I won't even try asking to make .ps1 files executable, because this had already been rejected for security reasons.

The other option is to embed Powershell script in a batch file and use a bootstrapper such as the following: 
@powershell "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof

The above contraption sort of works (except that positional arguments don't behave quite the same way, and I am sure that a lot of other small things are broken), however, if feels wrong that we have to jump through hoops like this.

I propose that there should be an officially supported way of bootstrapping.  How about adding a new powershell command line option, say -Batch, which behaves in the same way as -File, except that it skips first N lines of the file, and, unlike -File, does not complain about extension not being '.ps1'.  My bootstrapper could be then just: @powershell -Batch \"%~f0\" %* & goto :eof
Sign in to post a comment.