Accessing the Registry with PowerShellAccessing the Registry with PowerShellThere is a knack to accessing values in the registry with PowerShell. In the beginning navigation is deceptively difficult, but once you master the techniques it becomes reassuringly easy. Topics for PowerShell and Registry
♣ The Beginner's ConundrumAs a beginner people tell you that accessing the registry is as easy as accessing the file system. PowerShell does the work courtesy of a PSDrive provider, which opens the door to the registry. Thus you can type: CD HKLM:\ (Just as easy as when you type: cd C:\) I reminder that HKLM is an abbreviation of HKEY_LOCAL_MACHINE, which is well-known to PowerShell. There is also the users section of the registry at HKCU. To go back one step, you can see the connection between the registry and the file system by typing plain: Get-PSDrive Easy Ways of Accessing the Registry with PowerShella) Using familiar aliases cd HKLM:\ b) Get the same result as above, but using native PowerShell commands Set-Location HKLM:\ Learning Points Note 1: You need the colon, thus HKLM: (and not plain HKLM) Note 2: The backslash makes sure that you connect to the root of the registry. Note 3: Don't worry about the error message PermissionDenied to the SECURITY hive, that's normal. So far .... So goodSuperficially, the simple commands shown above work as expected. The problems start when you try to view values in the registry, and they get worse if you try and change Reg_SZ or DWORD setting. This is where analogies with the file-system break down, and we need to learn new techniques. Scenario: you want to check or enumerate the name of the logged on user. $RegKey ="Software\Microsoft\Windows NT\CurrentVersion\Winlogon" Note 1: To omit the dot (period) after -path is fatal. -path. is correct. Note 2: Here is an alternative shorter version $RegKey = "HKLM:\Software\Microsoft\Windows
NT\CurrentVersion\Winlogon" ItemProperty - An Important PowerShell Noun for the RegistryWe have already had a lucky break, because we've been tipped off there is PowerShell cmdlet called get-ItemProperty. Now we can exploit this knowledge by checking for similar nouns to ItemProperty. get-Command -noun ItemProperty Expected Results Eureka! Let us investigate Set-ItemProperty and see if it has any parameters to change settings in the registry. get-Help Set-ItemProperty -full Note 1: Do you see a parameter called -Value? Now we have the skill to change values in a named registry key. Changing CachedLogonsCountI have just chosen CachedLogonsCount almost at random, my greatest joy is when you modify this REG_SZ to a registry entry that you are interested in. Scenario - Let us increase Cached Logons to 50. If you haven't backed up at least the Winlogon portion of the
registry, please take this action before continuing: $RegKey ="HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" Learning Points Note 1: The crucial point is that we are using the verb 'Set' not 'Get'. Set-ItemProperty has the useful parameter -value. Note 2: On reflection, you can see how PowerShell mimics the registry's sections of: Key, Value, Data. However, slightly confusingly, Registry's Value = PowerShell -name. Furthermore, Registry's Data = PowerShell's -value.
Guy Recommends: SolarWinds LANSurveyor
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |