Skip Ribbon Commands
Skip to main content

Ondrej Sevecek's English Pages

:

Engineering and troubleshooting by Directory Master!
MCM: Directory

Quick Launch

Ondrej Sevecek's English Pages > Posts > The best way how to start PowerShell PS1 scripts
December 18
The best way how to start PowerShell PS1 scripts

If you have a PS1 script file, it is sometimes difficult to start it. You can always right-click it and select Run with PowerShell. But this may fail without much information displayed if the computer's effective execution policy prevents the script from running. When you create a scheduled task or other kind of scheduled job, you also have to explicitly call the powershell.exe with the script file parameter instead of just typing the name of the PS1 file.

This is the reason for which I always create and distribute another BAT file with all my PS1 scripts. I always create the two files with the same name, because then you do not need to specify the exact name of the PS1 file inside the calling BAT file:

@ECHO OFF
powershell -NoLogo -ExecutionPolicy Bypass -File "%~d0%~p0%~n0.p1" %*
exit /B %errorlevel%

The batch file calls PS1 with the same name as is its own filename. It also passes all its own command line parameters down to the powershell script. Finally it returns or forwards the exit code obtained from the powershell script.

I usually do not need to synchronize current directory to the parent folder of the script files, but if you like it, you can improve the batch slightly in this way:

@ECHO OFF
cd /D "%~d0%~p0"
powershell -NoLogo -ExecutionPolicy Bypass -File "%~d0%~p0%~n0.p1" %*
exit /B %errorlevel%

Comments

There are no comments for this post.

Add Comment

Title


You do not need to provide any value this column. It will automatically fill with the name of the article itself.

Author *


Body *


Type number two as digit *


This simple antispam field seems to work well. Just put here the number.

Attachments