PowerShell Not your Father’s Command Line Part 12 of 31: PowerShell and The Registry

imageIn yesterday’s post we took a look a quick look at providers.  In this post we are going to take a look at the registry.  Before I begin with this post I do have to give you the standard warning.  Working with and modifying the registry can sometimes produce undesired results.  Serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you know what your are doing and test your modifications before you go into production . For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs.

With that said, working with the registry provider in PowerShell has a tremendous amount of usefulness.  This allows you to script changes to the registry across multiple computers easily enabling you work more efficiently, So how do you get started, well like the registry the PowerShell provider allows you to work with both HKey Local Machine and HKey Current user and you can use either of the following commands to enter the areas of the registry you wish to work with in PowerShell:

  • cd HKLM:    For accessing  the local machine hive
  • cd HKCU: For accessing the current user hive

Just like working with environment variables we did yesterday, if you want to take a look at the registry values and entries, simply use dir to view the directories and then us cd (btw cd is an alias for Set-Location) to navigate through the registry.  So if you wanted to go check on your

Windows Media player preferences, which is in the current user hive your commands may look like the following:

cd HKCU:

cd software\microsoft\mediaplayer\preferences

You could also navigate each section individually (i.e. cd software, cd microsoft …etc), just like walking through directories in a file system.  If at time you want to see what is each section you can use dir.  However as you go through looking through the values you may have noticed you are not seeing any individual registry values.  This where you can use another “item” cmdlet to really get to the values, from the registry location of your choosing if you type:

Get-ItemProperty –path . *    or you can also use Get-ItemProperty *

This will show you all the registry values and settings from your current location or your other locations in the same location and sub-locations.   In the above command for path you may noticed a nifty little shortcut and it was not a typo.  In a path statement when you use a period “.” that means use your current location.   Your results will look something like the following:

image

This is where you can start to get to work with and changing the registry values you want to.  To change the values you can use the Set-ItemProperty cmdlet to accomplish the task.  This cmdlet will allow you to change existing values as well as add new values to the registry, it also supports the great little shortcut of using the “.” to refer to the local path.  For example if you wanted to change the shuffle mode on your media player (1 is on 0 is off) .  When you run the following command:

Set-ItemProperty -path . -name modeshuffle -value 1

Also if you were not actually at that location and still wanted to set the value you could run the following:

Set-ItemProperty -path HKCU:software\microsoft\mediaplayer\preferences -name modeshuffle -value 1

As you can see working with the registry can provide some value and again be very careful and make sure you bot all of your I’s and cross all of your t’s when working in the registry.  Thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell. Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day!