I've a batch file run.bat which calls a python script (same issue if it's a ps1 instead of py script)
Contents of run.bat
powershell.exe -executionpolicy unrestricted
powershell python .\aTest.py
This was working fine until today where batch file is not invoking the python script. The command window shows the following message: "Try the new cross-platform PowerShell https://aka/ms/pscore6"
I found from online that I can suppress this message with -nologon but that did not help other than removing the message. I removed the following line powershell.exe -executionpolicy unrestricted and script worked. There was no user permissions change or anything made to the system between the last time it was successful and today.
Why this is happening is puzzling me and initially the -executionPolicy was added because without it, the script wasn't running. Now it's the opposite, how can I figure out why this happened? What caused it? Are there any difference having the extra PS flags and not if the user is a local admin group?
System is a Windows 10 and has one local admin user.
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.
-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.