Set-ExecutionPolicy from inside an already running script

Topics: Archive - Deployment Scripts
Nov 23, 2014 at 2:50 PM
Hi,

I was aware running set-execution Policy was correct to make SCCM run a ps1 but this:

Set-ExecutionPolicy -ExecutionPolicy 'ByPass' -Scope 'Process' -Force -ErrorAction 'SilentlyContinue'

From Inside a PS1 what is it use to as the ps1 has already the right to be run?

Thanks,

François
Developer
Nov 23, 2014 at 8:09 PM
If you don't have execution policy set for your company via group policy or if you did not specify the execution policy when you launched PowerShell from the command line, then the script will set the execution policy. It is there to catch those scenarios where people don't know or forget to set the execution policy when they launch PowerShell of if they simply prefer to have the call made in the script. Also, it will not cause any problems if you set the execution policy at multiple points of the process.
Nov 23, 2014 at 9:47 PM
Hi,

If the script is running then the set-execution Policy is correctly set. Right? Once the script is start, I would think the set-execution Policy is not need?!
Or it is use for the try?

Thanks,

François
Developer
Nov 23, 2014 at 9:56 PM
No, the script can run without the execution policy being set. The execution policy must be set before you take any actions in the script. That's why it must be the first thing you do in your script. the try is needed because you can't modify the execution policy if is already set via GPO by your organization. It will throw an error if you try to modify the execution policy when a GPO is in effect.