Skip to content

Instantly share code, notes, and snippets.

@b-
Created August 21, 2017 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-/4f51ed6709fa198384139987fa9f140f to your computer and use it in GitHub Desktop.
Save b-/4f51ed6709fa198384139987fa9f140f to your computer and use it in GitHub Desktop.
PowerShell batch glue
:: I didn't do any testing on this. In fact, I typed it on my iPhone. There is probably something wrong with it. But I've typed this so many times I have muscle memory, hence my putting it up here.
:: Name this file (ps1-file-without-extension).bat, so if you have a script "myscript.ps1" you'd name this file "myscript.bat"
:: Flags and shit are optional. Read the PowerShell manual for more info. I think there's one called "windowstyle" or something which can be pretty useful.
:: n.b. this is a Windows batch file. Maybe I'm crazy, but I actually like PowerShell on *nix. However, this will not work on *nix.
:: I *think* that since PowerShell uses # to denote comments you could stick a shebang (e.g., `#!/usr/bin/powershell`) but I'm not sure if there really is a standardized install path, nor do I know if `#!/usr/bin/env powershell` would work, either. (Maybe I should give up powershell on *nix and just learn Python...)
::@echo off
set "flags=-executionPolicy bypass"
::set "flags=%flags% -windowstyle hidden"
powershell.exe %flags% "%~dpn0.ps1"
:: "%~dpn0" expands to the full path and filename without extension of THIS file.
:: how should the script handle errors? should it in the first place?
::if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL%
::pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment