powershell.exe -executionpolicy unrestricted
This enters an interactive PowerShell session that requires a user to interactively submit exit
in order to exit the session, and it is only then that the batch file continues executing.
-executionpolicy unrestricted
applies only to that session (process).
Since neither the -File
nor the -Command
parameter are being used (the latter possibly implicitly, by passing command(s) only), PowerShell emits a "logo", i.e. a copyright message:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Recent versions of Windows PowerShell append a message promoting the cross-platform, install-on-demand successor edition, PowerShell (Core) v6+, to this message, so that you'll see the following:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
Use -NoLogo
to suppress this output; however, as implied above, this is not necessary if you pass code to execute as part of the call, either by passing a script file path (.ps1
) to -File
(-f
), or by (possibly positionally) passing command(s) to -Command
(-c
). However, you do need -NoLogo
with -File
if you combine it with -NoExit
.
powershell python .\aTest.py
Generally speaking, there is no need to involve PowerShell in order to execute a Python script - directly invoking python .\aTest.py
from a batch file should do.
Only if the call to the Python script relies on initializations performed via PowerShell's profiles (notably via the current user's $PROFILE
file) would invocation via PowerShell be required.
- As an aside: use the
-NoProfile
CLI option in case you want to suppress loading of any profile files, which is usually the right thing to do, so as to ensure a predictable execution environment and avoid unnecessary processing.
If you do need to call via PowerShell, the effective execution policy does not apply to calling a Python script - it only applies to PowerShell scripts (*.ps1
); if the profile files happen to call PowerShell scripts, use the following:
powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py
Note: Bypass
bypasses all checks regarding execution of .ps1
scripts, whereas Restricted
would still prompt before executing scripts downloaded from the web.
Note: Using the -Command
(-c
) parameter name explicitly isn't strictly necessary with powershell.exe
, the Windows PowerShell CLI; however, pwsh.exe
, the PowerShell (Core) 6+ CLI, now does require it.