One of the great PowerShell features is that it treats the registry like any other location or directory. In PowerShell you can connect directly to the registry and navigate the key hierarchy just as if it as a logical drive with folders. I have a very brief demonstration script you can run to find the registered ower and organization for the local system. At this point in time (RC1), PowerShell's Registry provider doesn't support mapping to a remote system so this script has to be run in PowerShell locally. Here's the script, which is also available for download from my script library.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
#==============================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1
#
# NAME: Show-RegisteredUser.ps1
#
# AUTHOR: Jeffery Hicks , JDH Information Technology Solutions
# DATE : 8/23/2006
#
# COMMENT: Demonstration script for reading the registry in PowerShell
# KEYWORDS: PowerShell, Registry
#===============================================================
#Show-RegisteredUser.ps1
$path="HKLM:\Software\Microsoft\Windows NT\CurrentVersion"
$reg=Get-ItemProperty $path
#$reg #Uncomment this line if you want to see all of the available properties
Write-Host "Registered Owner:"$reg.RegisteredOwner
write-Host "Registered Organization:"$reg.RegisteredOrganization
Write-Host `n #write a blank line to make it easier to read
This is a short script and you could just as easily run each command interactively in PowerShell. A script just makes things easier. In this case the script will return the registered owner name and organization like this:
Registered Owner: Jeffery D. Hicks
Registered Organization: JDH Information Technology Solutions
How does it work?
The first thing we do is define a variable with the registry path. This is not required, it just makes the script easier to work with and re-usable. If at some later date I want information from another key, all I need to do is change the path and redefine the variable. You should be looking for ways in your scripts to modularize code and make it easier to reuse. But I digress...
The script's core is calling the Get-ItemProperty cmdlet. The cmdlet takes our path variable as a parameter. We save the cmdlet's results to a variable, $reg. This variable contains all the parameters for the given path. If you uncomment the next line you would see something like this:
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE
\Software\Microsoft\Windows NT\CurrentVersion
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE
\Software\Microsoft\Windows NT
PSChildName : CurrentVersion
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
CurrentBuild : 1.511.1 () (Obsolete data - do not use)
InstallDate : 1116119161
ProductName : Microsoft Windows XP
RegDone :
RegisteredOrganization : JDH Information Technology Solutions
RegisteredOwner : Jeffery D. Hicks
SoftwareType : SYSTEM
CurrentVersion : 5.1
CurrentBuildNumber : 2600
BuildLab : 2600.xpsp.051011-1528
CurrentType : Uniprocessor Free
CSDVersion : Service Pack 2
SystemRoot : E:\WINDOWS
SourcePath : D:\I386
PathName : E:\WINDOWS
ProductId : 55274-640-1614466-23527
DigitalProductId : {164, 0, 0, 0...}
LicenseInfo : {51, 183, 33, 193...}
SubVersionNumber :
However, I'm only after some specific properties. The $reg object that we created can show each registry setting as an object parameter. In PowerShell we can display that property as simply as typing $reg.ProductName. In the script we use:
write-Host "Registered Owner:"$reg.RegisteredOwner
write-Host "Registered Organization:"$reg.RegisteredOrganization
to display the information.
Again, the script makes it easier to follow what is happening, but you could just as easily get the owner or organization information with a one line command (it's a long line that wraps here but you would type it as one long expression):
(Get-ItemProperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").RegisteredOwner ; (Get-ItemProperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").RegisteredOrganization
In another post I'll show you how to modify the registry in PowerShell as we change these keys.
Technorati Tags : PowerShell, Scripting