powershellbatch-filecmdjscriptprivilege-elevation

Script to change PowerShell ExecutionPolicy


When I setup my system, I use a number of config scripts to have my cosy place to play.

Based on this, I run by double-clicking the following enableps.js:

new ActiveXObject("Shell.Application").ShellExecute(
   "powershell", "-noexit -Command \"& Set-ExecutionPolicy RemoteSigned\"",  "", "runas");

Because of the -noexit I can issue in the displayed PowerShell window:

Get-ExecutionPolicy 

and get as expected:

RemoteSigned

Unfortunately, when opening a new instance of PowerShell, the policy keeps to be Restricted.

If I run in a standard cmd prompt:

cscript  "path\to\enableps.js"

it works. But if I embed the command in the enableps.cmd batch and again try to run it by double-clicking, it doesn't work. If I right-click enableps.cmd and use the Runas-Administrator entry, it works again.

So how can I make things working with the standard double-click (plus the related Windows prompt)?


Solution

  • You need to run the command and give it the -Scope argument so that it applies to more than the current session. Add the argument:

    -Scope CurrentUser