put a simple .cmd file in my subfolder with my .ps1 file with the same name, so, for example, a script named "foobar" will have "foobar.ps1" and "foobar.cmd". Therefore, to run .ps1, all I have to do is click the .cmd file from Explorer or run .cmd from the command line. I use the same base name because the .cmd file will automatically search for .ps1 using its own name.
::==================================================================== :: Powershell script launcher ::===================================================================== :MAIN @echo off for /f "tokens=*" %%p in ("%~p0") do set SCRIPT_PATH=%%p pushd "%SCRIPT_PATH%" powershell.exe -sta -c "& {.\%~n0.ps1 %*}" popd set SCRIPT_PATH= pause
Pushd / popd allows you to run a .cmd file from the command line without having to change the specific directory where the scripts are located. It will change to the script directory, then return it to the original directory.
You can also turn off the pause if you want the command window to disappear when the script ends.
If my .ps1 script has parameters, I request them with GUI hints using .NET Forms, but also make the scripts flexible enough to accept parameters if I want to pass them. That way, I can just double-click on it from Explorer and not know the details of the parameters, as it will ask me what I need with lists or other forms.
KoZm0kNoT May 20 '15 at 18:57 2015-05-20 18:57
source share