Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Created December 9, 2016 02:00
Show Gist options
  • Save fearthecowboy/421bead0a52c1233c279a46101f5e52f to your computer and use it in GitHub Desktop.
Save fearthecowboy/421bead0a52c1233c279a46101f5e52f to your computer and use it in GitHub Desktop.
Make a powershell script with a .cmd (or .bat) extension
@powershell -noninteractive "& ([Scriptblock]::Create( ((gc -raw '%~df0') -replace '^@powershell.*' ) )) %*" & goto :eof
###
### Example : Just use that first line in a .cmd file and it will execute this file with powershell!
###
param( $name, $lastname )
write-host -fore green "Well, hello $name $lastname"
<# Notes:
this works just fine from cmd (or powershell) - even with named parameters:
C:\>test.cmd
Well, hello
C:\>test garrett serack
Well, hello garrett serack
C:\>test -lastname garrett -name serack
Well, hello serack garrett
###
Q: Why would you want to do this ?
A: Because, a lot of people, don't run powershell as their shell, and running a .PS1 file from cmd doesn't run the script
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment