2

What I am trying to do is to set -ExecutionPolicy (allow scripts) for the actual process (cmd.exe)

If a write this command in cmd.exe (as Administrator):

PowerShell.exe -ExecutionPolicy Unrestricted

Output is:

Windows PowerShell 
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.

Lernen Sie das neue plattformübergreifende PowerShell kennen – https://aka.ms/pscore6

Than I put on this command:

PowerShell.exe -Command "SomePowerShellScript.ps"

And all works.

If I try the same from a batch-file, it pauses after the first command.

The reason why I use the first command and not the Powershell specific with Set-ExecutionPolicy is described by Microsoft:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1

PowerShell.exe -ExecutionPolicy Unrestricted

--> sets ExecutionPolicy to cmd and powershell (script can be started)

PowerShell.exe -Command Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

--> sets Policy only on actual powershell.exe process (script is not allowed to start)

And for clarifing I don't want to set execution policy to Unrestricted for LocalMachine/CurrentUser if possible.

For explaning this problem, the batch-file is only for "lazy" people to directly start the Powershell-Script without having to set ExecutionPolicy (So it works by double clicking).

Has anyone an idea why the batch-file pauses after the first command (I also tried it with call powershell.exe)?

Thanks for a response :).

2
  • Why do you use a Batch file? Much more easier is to create a simple link (shortcut) file: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -file "D:\mypowershell\myscript.ps1" Commented Jan 9, 2020 at 11:19
  • PowerShell.exe -ExecutionPolicy Unrestricted -Command "SomePowerShellScript.ps" should work smoothly. Commented Jan 9, 2020 at 11:53

1 Answer 1

4

The apparent pause is a result of PowerShell.exe -ExecutionPolicy Unrestricted actually entering an interactive PowerShell session, which you must manually exit (aside from that, the execution policy is set for that session (process) only).

If you place -ExecutionPolicy Unrestricted (or -ExecutionPolicy Bypass) before a -File argument, the execution policy is set - for the process only - before execution of the script file passed to -File is attempted, and execution should succeed (unless a group policy preempts overriding the persistently configured execution policy on the command line:

powershell.exe -ExecutionPolicy Unrestricted -File SomePowerShellScript.ps1

You can also use -Command, but note that this changes the interpretation of the arguments; notably, you must then use .\SomePowerShellScript.ps1 to refer to the script to execute.

See this answer for more information.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.