0

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)?

antonio
  • 10,629
  • 13
  • 68
  • 136

3 Answers3

1

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
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
1

... and the solution is:

Double click on the file:

// enableps.js
// -----------

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

Consider replacing RemoteSigned with Unrestricted to allow running downloaded scripts too.

Credit goes to TheMadTechnician, who anyway did not write the full code.

Community
  • 1
  • 1
antonio
  • 10,629
  • 13
  • 68
  • 136
  • While I'm fairly fluent in PowerShell and it's workings I am unfamiliar with Java scripting, so I was not comfortable writing the full code in a language that I do not know. If you have looked at my previous answers you will see that I am far from too lazy to write complete answers. – TheMadTechnician Oct 06 '14 at 20:33
  • @TheMadTechnician: I edited it, anyway it was intended as a joke) – antonio Oct 07 '14 at 07:45
  • Sorry man, was my first day at a new job and was probably a bit high strung. Didn't mean to take it out on you. – TheMadTechnician Oct 07 '14 at 17:58
0

By default making system-wide changes will require you to elevate the process if it hasn't been elevated already. If you want your script to disable the execution policy at the machine level then you will either need to switch off UAC or else you will have to run your cscript using a shellExecute, which will present the user with the required UAC approval dialog.

Stephen Connolly
  • 1,639
  • 10
  • 15
  • Right Stephen, but before, despite the UAC dialogue, the change was not persistent. With the `-Scope` option it is. – antonio Oct 07 '14 at 07:49