August 14, 2016 at 6:48 pm #50364
have a question:
How change execution policy during script run
I run some script, but before I need change execution policy and in the end change policy back on default
$policy = "Unrestricted"
if((get-executionpolicy) -ne $policy)
Set-executionpolicy $policy -force
next I doing some code here
and in the end of script I change policy on restricted
Now, problem when script run it change policy but rest script fail
but if I re-run script again(since policy already changed on unresticted) it run ok
How I can make it work?
August 15, 2016 at 12:19 pm #50448
You can call the script by setting the ExecutionPolicy for that session like below.
powershell -ExecutionPolicy Unrestricted script.ps1
Entire script will run with that policy.
You can check it in this way
PS C:\>Set-ExecutionPolicy Restricted -Force PS C:\>'Get-ExecutionPolicy' | Out-File ~\TestPolicy.ps1 PS C:\>powershell -ExecutionPolicy Unrestricted ~\TestPolicy.ps1
You will get the output as Unrestricted even you had set i t to Restricted.
- This reply was modified 10 months, 2 weeks ago by kvprasoon. Reason: Code Formatting
August 15, 2016 at 4:24 pm #50477
If you are currently using an Execution Policy that doesn't allow you to run scripts, for whatever reason, you cannot change the policy inside a script since it wouldn't execute. If you can change the policy from within a script, you really don't need to do it.
August 15, 2016 at 5:01 pm #50480
This affects only the current Windows PowerShell process (window). Run this before your script.
Set-ExecutionPolicy Unrestricted -Scope Process -Force
August 16, 2016 at 5:23 pm #50629
I always put this batch file into the same folder as my .ps1 and use it to run my scripts since I get tired of setting the policy whenever I have to go to a new computer.
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "";
August 16, 2016 at 5:31 pm #50636
Won't let me edit my post and fix it so this is the link I used to get the batch file:
August 21, 2016 at 6:02 pm #51367
Thank you every one
sorry for late update,
PowerShell -NoProfile -ExecutionPolicy Unrestricted
this one work for me
You must be logged in to reply to this topic.