Change execution policy

This topic contains 6 replies, has 5 voices, and was last updated by Profile photo of Brad Brad 1 month, 1 week ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #50364
    Profile photo of Brad
    Brad
    Participant

    Hi,
    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
    Example:
    $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?
    Thanks.

    #50448
    Profile photo of kvprasoon
    kvprasoon
    Participant

    Hi Brad,

    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.

    Regards,
    Kvprasoon

    • This reply was modified 1 month, 2 weeks ago by Profile photo of kvprasoon kvprasoon. Reason: Code Formatting
    #50477
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    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.

    #50480
    Profile photo of random commandline
    random commandline
    Participant

    This affects only the current Windows PowerShell process (window). Run this before your script.

    Set-ExecutionPolicy Unrestricted -Scope Process -Force
    #50629
    Profile photo of keith noble
    keith noble
    Participant

    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.

    @ECHO OFF
    SET ThisScriptsDirectory=%~dp0
    SET PowerShellScriptPath=%ThisScriptsDirectory%RunMev33.ps1
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "";

    • This reply was modified 1 month, 2 weeks ago by Profile photo of keith noble keith noble.
    • This reply was modified 1 month, 2 weeks ago by Profile photo of keith noble keith noble.
    #50636
    Profile photo of keith noble
    keith noble
    Participant

    Won't let me edit my post and fix it so this is the link I used to get the batch file:
    http://stackoverflow.com/questions/23156707/pass-variable-from-batch-to-powershell

    #51367
    Profile photo of Brad
    Brad
    Participant

    Thank you every one
    sorry for late update,

    PowerShell -NoProfile -ExecutionPolicy Unrestricted
    this one work for me

    Thanks.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.