Edit the Registry on Multiple Remote Computers with PowerShell

Edit the Registry on Multiple Remote Computers with PowerShell

  • Comments 6
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to edit the registry on multiple remote computers with one command.

Microsoft Scripting Guy, Ed Wilson, is here. The PowerShell Essentials for the Busy Admin series was a huge success. Not only were there a lot of people who attended the live meetings, but there will be way people who will download and use the recordings in the future. If you were able to attend, I hope you enjoyed the series as much as I enjoyed making the presentations. Doing the presentations does take a lot of work, and requires a lot of time, but it is worthwhile.

On Monday, I am speaking for International PowerShell User Group Day. I will be doing one of the presentations from the Charlotte Windows PowerShell user group meeting—this will be a blast. On Friday, I am speaking at the Charlotte IT Pro Appreciation Day conference in Charlotte. Nearly a thousand people will be attending this all day event. The Scripting Wife is helping out with registration, so if you are in the area, you will probably get a chance to see her. The following week I am speaking at the TechStravaganza in New York. It seems that there is something nearly every week from now until TechEd. Speaking of Microsoft TechEd 2010, the Scripting Wife and I will be there manning the Scripting Guys booth. If you have not registered, you should. It will be a great event. Besides the event, I grew up in Florida, and I relish any occasion to head back down south. If you are having a hard time keeping up with the schedule—so am I. This is why I add all the events to the community page on the Script Center.

Anyway…

Yesterday, I talked about Use PowerShell to Edit the Registry on Remote Computers. I used the same technique that I used to create a registry key on a local computer by using the Registry provider.

The technique I illustrated worked just fine for setting a registry key and value on a single remote computer. But if more than one computer needs the change, the method of entering a remote Windows PowerShell session is a bit cumbersome. Clearly, it is better than a lot of mousing around; but still, it is cumbersome.

One way to avoid this and to enable the ability to make changes on multiple machines is to turn the commands into a single line. It is then really easy to run the command by using the Invoke-Command cmdlet. To do this, I use the following steps:

  1. Use the Get-Credential cmdlet to retrieve cmdlets. Store the returned credential in a variable.
  2. Use the Invoke-Command cmdlet to run the command on remote computers.

The two commands are shown here. Keep in mind that the second command is one long command that has wrapped over several different lines. I have not included line continuation for this command.

$cred = Get-Credential iammred\administrator

Invoke-Command -cn dc3 -cred $cred {pushd;sl HKCU:\software; ni HSG; New

-ItemProperty -name forscripting -PropertyType string -path hsg -value "powershell rocks" ; popd}

The registry key is the same registry key and registry property that I created in yesterday’s blog. I test the command, and the commands and the output associated with the commands are shown in the image that follows.

Image of command output

Now, I decide to test the command against two computers. The only change required is to add an additional computer name to the -cn property (I deleted the registry key on DC3 prior to running the code the second time). This is shown here.

Image of command output

I decide to make one more test. I have a text file that contains the names of some of the servers on my network. The file is Servers.txt. Each line of the file contains a different computer name, as shown here.

Image of file

I can use the Get-Content (gc is an alias) cmdlet to read the contents of the text file. Instead of specifying a particular computer name, I can use Get-Content to retrieve the computer names for me. The revised command is shown here.

$cred = Get-Credential iammred\administrator

Invoke-Command -cn (gc c:\fso\servers.txt) -cred $cred {pushd;sl HKCU:\software; ni HSG; New-ItemProperty -name forscripting -PropertyType string –path hsg -value "powershell rocks" ; popd}

The image that follows illustrates these commands, and the output associated with the commands.

Image of command output

Well, that is about it for today. Join me tomorrow when I will talk about testing for registry keys and deleting them.

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • @Ed - Very useful.

  • Hi Ed,

    short but good!

    You can do great things in one line of Powershell code!

    Remoting and the registry provider are good examples for special powershell goodness.

    PS rocks :-)

    Klaus.

  • yep, nice and simple. Powershell rules, then comes CMD and REGEDIT.

    I also like gpedit.msc

  • How about modifying ACLs on the registry keys?

  • @ZipFile - Would beuseful but not possible yet short of using a custom Type written in C or C#

    SubinAcl can do it.

  • I luv power-shell, Just want to share the script that I have used in our environment for modifying the registry & it reports back in HTML emaiil.

    http://msexchange.me/2014/01/04/readwrite-registry-for-multiple-servers-via-powershell-part2/

    uploaded on scriptcenter as well.

    http://gallery.technet.microsoft.com/Write-Create-Modify-2952426d