hello
Google
Welcome to Carpe Diem: Flaphead@Home Sign in | Join | Help

Carpe Diem: Flaphead.com

Seize the Day

News


  • Add to Technorati Favorites <script type="text/javascript" src="http://technorati.com/embed/3ni3q36ikc.js"> </script>
    This information is provided "AS IS" with no warranties, and confers no rights. Also some of the information contains my views and thoughts.
    <script src="http://widgets.technorati.com/t.js" type="text/javascript" charset="UTF-8"></script>

    Add Me! - Search Engine Optimization

    I heart FeedBurner

Powershell and the Registry

Brett touched on this a while back, and I just wanted to pad things out a bit.

So as brett mentioned, you can access the registry like a file system in powershell. 

So start up powershell and type:

PS C:\> sl HKLM:
PS HKLM:\>

Now you can sl, cd and chdir are all an alias for Set-Location, so you can use either.  You can see the aliases for Set-Location by typing:

PS HKLM:\> get-alias | where-object {$_.Definition -eq "Set-Location"}

CommandType     Name                            Definition
-----------     ----                            ----------
Alias           sl                              Set-Location
Alias           cd                              Set-Location
Alias           chdir                           Set-Location

Now you can type dir and navigate the registry like the file system using cd etc etc.  How cool is that.  You can also get it to “auto fillin” by using <TAB>.

So that is okay, but what about the properties of keys?  Well get to the key you want and show it’s properties like this:

PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters> get-itemproperty .

PSPath                  : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHIN E\SYSTEM\CurrentControlSet\Services\NetBT\Parameters
PSParentPath            : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT
PSChildName             : Parameters
PSDrive                 : HKLM
PSProvider              : Microsoft.PowerShell.Core\Registry
NbProvider              : _tcp
NameServerPort          : 137
CacheTimeout            : 600000
BcastNameQueryCount     : 3
BcastQueryTimeout       : 750
NameSrvQueryCount       : 3
NameSrvQueryTimeout     : 1500
Size/Small/Medium/Large : 1
SessionKeepAlive        : 3600000
TransportBindName       : \Device\
EnableLMHOSTS           : 1
DhcpNodeType            : 4

If you want a specific property you can get it by typing:

PS C:\> $(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters).DHCPNodeType
4

so this is cool too.  So now in a cmdlet I want to store a variable from a registry key entry.  How do I do that?  Well the same as above

PS C:\> $regkey = $(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters).DHCPNodeType
PS C:\> $regkey
4

Okay, so what else.  You can do this:

PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters> dir -recurse

and it wil list all the folders and subfolder under the key.  What I have not been able to find yet is get an output similar to reg.exe /enum.  If anyone know hows to let me know

Anyway so this is how you read from the registry, what about writing?.  So create a new key just do this:

PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT> md test

   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT

SKC  VC Name                           Property
---  -- ----                           --------
  0   0 test                           {}

PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\test> New-ItemProperty . -name Fred -value "hello"

PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\test
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT
PSChildName  : test
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry
Fred         : hello

and if I want to change the value for Fred

PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\test> Set-ItemProperty . -name Fred -value "goodbye"
PS HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\test> Get-ItemProperty .

PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\test
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT
PSChildName  : test
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry
Fred         : goodbye

How cool is that.  Hope this helps more to come soon

Posted: 08 September 2006 17:00 by Paul Flaherty

Comments

Lee said:

Paul -- I don't see a "/enum" option for reg.exe. What does it do?

C:\>reg

Console Registry Tool for Windows - version 3.0
Copyright (C) Microsoft Corp. 1981-2001. All rights reserved


REG Operation [Parameter List]

Operation [ QUERY | ADD | DELETE | COPY |
SAVE | LOAD | UNLOAD | RESTORE |
COMPARE | EXPORT | IMPORT ]

Return Code: (Except of REG COMPARE)

0 - Succussful
1 - Failed

For help on a specific operation type:

REG Operation /?

Examples:

REG QUERY /?
REG ADD /?
REG DELETE /?
REG COPY /?
REG SAVE /?
REG RESTORE /?
REG LOAD /?
REG UNLOAD /?
REG COMPARE /?
REG EXPORT /?
REG IMPORT /?

If you tell me that, then I can tell you how to get its output :)

Lee
# September 9, 2006 06:36

Flaphead on TechNet said:

Originally Posted here: http://blogs.flaphead.dns2go.com/archive/2006/09/08/3548.aspx Brett touched...
# September 12, 2006 10:22
New Comments to this post are disabled