Saturday, June 28, 2008

This little bit of usefulness in powershell is also available in various guides and books, but I often find that technical documentation is written to be more complete than practical.  I will scan a long document full of reference data to assemble what I need to do which can really be communicated in a simple example.  So here's a few easy ways to check to see if a registry entry exists or to look up a value.  These are really handy for testing and auto configuring machines on your network.

# The Test-Path cmdlet is handy for detecting not only if files exist in the file system, 
# but reg key entries as well
#
# $QuickLog will get a $true or $false
#
$QuickLog=(Test-Path 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log')
 
# This returns an array of all the values at this key
$regObj = get-item 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log'
 
# This is a path for using with set-ItemProperty
$path = 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log'
 
# Notice we use the object here
$keys = $regObj.GetValueNames()
foreach($key in $keys)
{
    # Do what you will with your reg keys
    if($key.Contains("Hints"))
    {
        # Set the execution policy so we can write here
        set-itemproperty -Path $path -Name ExecutionPolicy -Value unrestricted
        
        # This assumes you have a String property
        # Notice that we use the path and not the object here
        set-itemproperty -path $path -name $key -value 0
    }
}
Saturday, June 28, 2008 1:26:11 PM (Central Standard Time, UTC-06:00)