powershellbatch-filepsexecutionpolicy

Why removing powershell.exe -executionpolicy unrestricted suddenly working but wasn't before


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.


Solution

  • powershell.exe -executionpolicy unrestricted

    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.

    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.