Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if registry value exists

In my powershell script I'm creating one registry entry for each element I run script on and I would like to store some additional info about each element in registry (if you specify optional parameters once then by default use those params in the future).

The problem I've encountered is that I need to perform Test-RegistryValue (like here--see comment) but it doesn't seem to do the trick (it returns false even if entry exists). I tried to "build on top of it" and only thing I came up is this:

Function Test-RegistryValue($regkey, $name)  {     try     {         $exists = Get-ItemProperty $regkey $name -ErrorAction SilentlyContinue         Write-Host "Test-RegistryValue: $exists"         if (($exists -eq $null) -or ($exists.Length -eq 0))         {             return $false         }         else         {             return $true         }     }     catch     {         return $false     } } 

That unfortunately also doesn't do what I need as it seems it always selects some (first?) value from the registry key.

Anyone has idea how to do this? It just seems too much to write managed code for this...

like image 921
Newanja Avatar asked Apr 13 '11 12:04

Newanja


People also ask

How do I check if a reg exists?

You can search online via number plate sales specialist websites – like us right here at New Reg. Simply enter the exact registration you want to enquire about into the search box, and if it is available then the plate and price for it will show right at the top of the list of options.

How do I check registry values?

To access it, you have to click on Edit menu and select Find. The Find box will let you search for items in the Windows Registry, and this includes Keys, Values, and Data. You can also set it to match whole strings only. Hitting the F3 key will take you to all the found searches, one after the other.

How do I view the registry in PowerShell?

You can browse the registry tree the same way you navigate your drives. HKLM:\ and HKCU:\ are used to access a specific registry hive. Those, you can access the registry key and their parameters using the same PowerShell cmdlets that you use to manage files and folders.

How do I find my registry in command prompt?

To open Windows registry in the command prompt, press Windows key + R , type cmd, and press Enter . In the Command Prompt, type regedit and press Enter .


1 Answers

Personally, I do not like test functions having a chance of spitting out errors, so here is what I would do. This function also doubles as a filter that you can use to filter a list of registry keys to only keep the ones that have a certain key.

Function Test-RegistryValue {     param(         [Alias("PSPath")]         [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]         [String]$Path         ,         [Parameter(Position = 1, Mandatory = $true)]         [String]$Name         ,         [Switch]$PassThru     )       process {         if (Test-Path $Path) {             $Key = Get-Item -LiteralPath $Path             if ($Key.GetValue($Name, $null) -ne $null) {                 if ($PassThru) {                     Get-ItemProperty $Path $Name                 } else {                     $true                 }             } else {                 $false             }         } else {             $false         }     } } 
like image 164
JasonMArcher Avatar answered Sep 25 '22 02:09

JasonMArcher