Suzy Ikram
Suzy Ikram

Reputation: 21

Why i can't change the policy from restricted to remote signed?

enter image description here

I hade a problem with 'touch' command , so someone tolled me that i should change the policy (in PowerShell) so I can run the script in vs code ,but the problem is that i couldn't change the policy from restricted to remote signed .

Upvotes: 2

Views: 138

Answers (1)

mklement0
mklement0

Reputation: 440471

As the error message implies, your command requires elevation, i.e. must be run with administrative privileges.

The reason is that Set-ExecutionPolicy defaults to the LocalMachine -Scope value, which - due to making changes at the machine level, i.e. for all users - requires elevation.

Therefore, you have two options:

  • Either: Run your Set-ExecutionPolicy call from an elevated session:

    • Note:

      • To launch an elevated session, you either need to be a member of the Administrators group yourself, or you need an administrator to provide their credentials when prompted.
    • Interactively, you can create an elevated session by right-clicking on the PowerShell icon in the Start Menu or taskbar and selecting Run as Administrator

    • Programmatically, you can run the following from a (non-elevated) session (still requires interactive confirmation / authorization for elevation):

      Start-Process -Verb RunAs (Get-Process -Id $PID).Path '-Command Set-ExecutionPolicy -Force RemoteSigned'
      
  • Or: Run the Set-ExecutionPolicy with -Scope CurrentUser, in which case elevation is not required:

    • Note:

      • As the scope name implies, this only persistently changes the execution policy for the current user.
    • Run the following:

      Set-ExecutionPolicy -Force -Scope CurrentUser RemoteSigned
      

Note: -Force in the commands above suppresses the confirmation prompt that Set-ExecutionPolic presents by default.


For a comprehensive overview of PowerShell's execution policies, see this answer.

Upvotes: 2

Related Questions