#Requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # # # "Those who necglect to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** Function Test-RegistryItem { <# .SYNOPSIS Test the the existence of a registry value .DESCRIPTION This command will determine if a given registry key exists. You must specify a registry path and the name of the key. .PARAMETER Path The registry path using the registry PSDrive format, ie HLKLM: .PARAMETER Property The property name to check. The parameter as an alias of Name. .PARAMETER UseTransaction Use an active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions. .EXAMPLE PS C:\> test-itemproperty -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Property "Shell" True .INPUTS [String] .OUTPUTS [Boolean] .NOTES NAME: Test-RegistryItem AUTHOR: Jeffery Hicks VERSION: 1.0 LASTEDIT: 10/01/2010 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) .LINK Get-ItemProperty .LINK http://jdhitsolutions.com/blog #> [cmdletbinding()] Param ( [Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a registry path using the PSDrive format.")] [ValidateNotNullOrEmpty()] [string]$Path, [Parameter(Position=1,Mandatory=$True,HelpMessage="Enter a registry key name.")] [ValidateNotNullOrEmpty()] [Alias("name")] [string]$Property, [Switch]$UseTransaction ) Write-Verbose ("Looking for {0} in {1}" -f $Property,$Path) if (Test-Path $path) { if ($UseTransaction) { $item=Get-ItemProperty -Path $path -Name $property -ErrorAction "SilentlyContinue" -UseTransaction } else { $item=Get-ItemProperty -Path $path -Name $property -ErrorAction "SilentlyContinue" } if ($item ) { #display the item if using -Verbose Write-Verbose ($item | select * | Out-String) $True } else { Write-Verbose "Not found" $False } } else { Write-Warning "Failed to find $path" $False } } #end function