5

I would like some help on setting a registry value for powershell. The path is

[hkey_local_machine\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell] "Path"="c:\windows\system32\windowspowershell\v1.0\powershell.exe" "ExecutionPolicy"="unrestricted"

Since I have imported this from a known good machine,when I run the .reg file it works fine. But I would like this in a batch file.

When I manually call the command prompt, drop in the path e.g c:\powershell.reg, this imports the values and overwrites the the registry settings as required.
However if l call do the same thing in a batch file, the values are not changed in the registry Using the reg add command in a batch file the values does not work.

3
  • 3
    “Does not work” is not a sufficient description of what’s happening. – Daniel B Mar 31 '15 at 7:06
  • Hi perhaps I should have provided a little information. So in essence if I manually call the command prompt, drop in the path e.g c:\powershell.reg, this imports the values and overwrites the the registry settings as required. However if l call do the same thing in a batch file, the values are not changed in the registry. So batch files is as simple as – Papoli Mar 31 '15 at 17:57
  • Is group policy not an option? – Colyn1337 Apr 10 '15 at 17:35
11

Why not simply run the following via CMD

powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"

or this directly in Powershell (that's what the command is for after all):

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
0
1

How do I change Powershell Execution Policy silently from a batch script?

Warning:

The instructions below contain steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly.

Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs.

For more information see How to back up and restore the registry in Windows.


reg solution

@echo off
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "Path" /d "c:\windows\system32\windowspowershell\v1.0\powershell.exe"
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "ExecutionPolicy" /d "unrestricted"

regedit solution

@echo off
regedit /s file.reg

Where file.reg contains the following:

[hkey_local_machine\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell] 
"Path"="c:\windows\system32\windowspowershell\v1.0\powershell.exe"
"ExecutionPolicy"="unrestricted"

Note:

  • [/s|-s]

    When a filename is specified on the command line, this switch is used to suppress any informational dialog boxes that would normally be displayed. This is useful when the Setup program for an application wants to execute REGEDIT.EXE with a .REG file, but does not want the user to be confused by any dialog boxes that are displayed.


Further reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • reg - Read, Set or Delete registry keys and values, save and restore from a .REG file.
  • regedit - Import, export or delete registry settings from a text (.REG) file.
  • regedit - Command-line switches.
1
  • Thank you. I tried the Regedit.exe /s command and that failed. I will give that another go. Thank you – Papoli Mar 31 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.