Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to edit the registry on remote computers.
Microsoft Scripting Guy, Ed Wilson, is here. In Wednesday’s PowerShell Essentials live meeting presentation, one of the questions revolved around working with the registry on a remote computer. While there are lots of ways to work with the registry on a remote computer, including using Windows Management Instrumentation, or .NET Framework classes, I like to use a combination of Windows PowerShell remoting and the Windows PowerShell registry provider because it is easier. In fact, by using Windows PowerShell remoting, it is just as easy to work with the remote registry as it is to work with a local registry.
Note For a good introduction to using Windows PowerShell to work with the registry, see The Scripting Wife, Windows PowerShell, and the Registry. For more advanced topics, check out some of the other blog posts about the registry in the Hey, Scripting Guy! Blog archives. There you will find blogs such as:
Suppose I want to create a new registry key under HKEY_CURRENT_USER under the Software key, and I want to call it HSG. The registry location (for the target HSG) is shown in the following image.
To create the new registry key, I use the four steps:
The commands are shown here.
PS C:\> pushd
PS C:\> Set-Location HKCU:\Software
PS HKCU:\Software> New-Item -Name HSG
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 0 HSG {}
PS HKCU:\Software> popd
PS C:\>
The newly created registry key is shown in the image that follows.
To add a registry property value, I use the New-ItemProperty cmdlet. I perform the same basic steps I used to create the registry key, but I use the New-ITemProperty cmdlet instead of the New-Item cmdlet, as follows:
The use of these techniques is shown here.
PS HKCU:\Software> New-ItemProperty -Name forscripting -PropertyType string -path hsg -Value "PowerShell Rocks"
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\hsg
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software
PSChildName : hsg
PSDrive : HKCU
PSProvider : Microsoft.PowerShell.Core\Registry
forscripting : PowerShell Rocks
The newly created registry property is shown in the image that follows.
After I have done all this locally, it is really easy to do it against a remote computer. I can do it by using Windows PowerShell remoting by using the following steps:
The following image illustrates this technique.
I then use Remote Desktop to connect to the remote server to verify that the registry key and property are updated. This is shown in the following image.
Well, that is about it for creating a remote registry key. Join me tomorrow, when I will talk about doing this in a single command—a scenario that is useful when you need to make changes on multiple computers.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
##Method #1:
$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $computername)
$key=$reg.CreateSubKey('software\hsg')
$key.SetValue('scriptingfun','yes it is')
$key.Close()
$reg.Close()
##Method #2:
$sb={
new-item hkcu:\software\hsg
new-itemproperty -Path hkcu:\software\hsg -Name test -Value 1
}
Invoke-Command -ScriptBlock $sb -ComputerName $cpmputer -Credential $cred
What if I have to add a key for the current user which is not me? I.E Bill is logged into computer A and I need to add an entry into his HKCU hive. When I enter the pssession with my credentials doesn't it add the key to my registry hive on the remote computer?
Thanks,
Stanz