Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to work with the registry keys and values of a remote computer.
To work with the registry of a remote computer, use the scripts provided in this
chapter: Get-RemoteRegistryChildItem
(Recipe 21.13), Get-RemoteRegistryKeyProperty
(Recipe 21.14), and Set-RemoteRegistryKeyProperty
(Recipe 21.15). These scripts require that the remote computer has the remote registry service enabled and running. Example 21-5 updates the PowerShell execution policy of a remote machine.
PS > $registryPath = "HKLM:\Software\Microsoft\PowerShell\1" PS > Get-RemoteRegistryChildItem LEE-DESK $registryPath SKC VC Name Property --- -- ---- -------- 0 1 1033 {Install} 0 5 PowerShellEngine {ApplicationBase, ConsoleHost... 2 0 PowerShellSnapIns {} 1 0 ShellIds {} PS > Get-RemoteRegistryChildItem LEE-DESK $registryPath\ShellIds SKC VC Name Property --- -- ---- -------- 0 2 Microsoft.PowerShell {Path, ExecutionPolicy} PS > $registryPath = "HKLM:\Software\Microsoft\PowerShell\1\" + "ShellIds\Microsoft.PowerShell" PS > Get-RemoteRegistryKeyProperty LEE-DESK $registryPath ExecutionPolicy ExecutionPolicy --------------- Unrestricted PS > Set-RemoteRegistryKeyProperty LEE-DESK $registryPath ` "ExecutionPolicy" "RemoteSigned" PS > Get-RemoteRegistryKeyProperty LEE-DESK $registryPath ExecutionPolicy ExecutionPolicy --------------- RemoteSigned
Although this specific task is perhaps better solved through PowerShell’s Group Policy support, it demonstrates a useful scenario that includes both remote registry exploration and modification.
If the remote computer does not have the remote registry service running (but does have WMI enabled), you can use WMI’s StdRegProv
class to work with the registry as well. The following example demonstrates how to get and set the registry key that controls Remote Desktop:
$HKEY_CLASSES_ROOT
=
[Convert]
::
ToUInt32
(
80000000
,
16
)
$HKEY_CURRENT_USER
=
[Convert]
::
ToUInt32
(
80000001
,
16
)
$HKEY_LOCAL_MACHINE
=
[Convert]
::
ToUInt32
(
80000002
,
16
)
$HKEY_USERS
=
[Convert]
::
ToUInt32
(
80000003
,
16
)
$HKEY_CURRENT_CONFIG
=
[Convert]
::
ToUInt32
(
80000005
,
16
)
## Connect to the registry via WMI
$reg
=
Get-CimClass
-ComputerName
LEE-DESK
`
-Namespace
root
\
default
StdRegProv
## Get and set DWORD values on the remote machine
$reg
|
Invoke-CimMethod
-Name
GetDWORDValue
-Arguments
@{
hDefKey
=
$HKEY_LOCAL_MACHINE
;
sSubKeyName
=
"SYSTEM\CurrentControlSet\Control\Terminal Server"
;
sValueName
=
"fDenyTSConnections"
}
$reg
|
Invoke-CimMethod
-Name
SetDWORDValue
-Arguments
@{
hDefKey
=
$HKEY_LOCAL_MACHINE
;
sSubKeyName
=
"SYSTEM\CurrentControlSet\Control\Terminal Server"
;
sValueName
=
"fDenyTSConnections"
;
uValue
=
0
}
For more information about the Get-RemoteRegistryChildItem
, Get-RemoteRegistryKeyProperty
, and Set-RemoteRegistryKeyProperty
scripts, see Recipes 21.13, 21.14, and 21.15.
Recipe 21.13, “Program: Get Registry Items from Remote Machines”
Recipe 21.14, “Program: Get Properties of Remote Registry Keys”
Recipe 21.15, “Program: Set Properties of Remote Registry Keys”