Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this hash table in a PowerShell script (shortened with a few examples but contains about 8 or so items in it):
--Code previous to this hash table builds the $i variable so I will just supply it for this example of what it should be. It is dependent upon what edition of SQL is on the server. So SQL 2008 R2 default instance would be "MSSQL10_50.MSSQLServer".

$i = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER'
$SQLPaths = @{
   'DefaultData'=(Get-ItemProperty "$i\MSSQLServer").DefaultLog;
   'RepWorking'=(Get-ItemProperty "$i\Replication").WorkingDirectory

My question/issue...Is there a shortcut way of testing this registry path (Test-Path) within the hash table before it sets the value, or tries to? I have some keys just like this that if the feature or setting was never set then the key will not actually exist. Which is fine but how can I ignore it if it does not, and just let the value be null in the hash table?

share|improve this question
What value would be added to the hash table if the key does not exist? (I can see a solution if the answer isn't "don't add the key.) – Richard Aug 19 '11 at 16:51
They all contain directory paths for various locations of SQL Server configuration and user files. – Shawn Melton Aug 19 '11 at 17:51
Try #2: you want to test the registry path before setting the hash value. But what to do if the test fails (path doesn't exist)? – Richard Aug 19 '11 at 18:43
ah, sorry read it to quickly...setting it to NULL is fine. – Shawn Melton Aug 19 '11 at 19:33
up vote 1 down vote accepted

In PowerShell the if statement can be used as an expression:

$i = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER'
$SQLPaths = @{
   'DefaultData'= if (Test-Path "$i\MSSQLServer") { 
                     (Get-ItemProperty "$i\MSSQLServer").DefaultLog
                  } else {
   'RepWorking'=(Get-ItemProperty "$i\Replication").WorkingDirectory

Also if you're using the PowerShell Community Extensions (PSCX) it adds a Invoke-Ternary (alias ?:) function which could make this less verbose:

$i = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER'
$SQLPaths = @{
   'DefaultData'= ?: {Test-Path "$i\MSSQLServer"} `
                     { (Get-ItemProperty "$i\MSSQLServer").DefaultLog } `
                     { $null };
   'RepWorking'=(Get-ItemProperty "$i\Replication").WorkingDirectory
share|improve this answer
I would actually need to test the path down to the registry key. $i\MSSQLServer will exist but the registry key itself might not. – Shawn Melton Aug 22 '11 at 20:23
Ah never mind, just found that Test-Path is not capable of checking if registry key exist...oh well. – Shawn Melton Aug 22 '11 at 20:37
work around I believe if ((Get-ItemProperty "$i\MSSQLServer").DefaultLog -ne $null) {get value} else {$null} – Shawn Melton Aug 22 '11 at 20:44
Test-Path certainly works with the registry here. But note it will test registry keys. To check a value you need to use Test-Path on the containing key, and then see if Get-ItemProperty on the value (normally it would fail but adding -ErrorAction SilentlyContinue will lead to a $null). Of course as this logic is non-trivial it can be wrapped in a re-usable function. – Richard Aug 23 '11 at 6:41
I found out the replication portion, $i\Replication, will not exist at all so just doing Test-Path on it works well. Apparently if you do Get-ItemProperty on a registry entry and it does not exist, it will return $null by default, it will not show any errors. Which the $i\MSSQLServer will always exist on servers with SQL Server installed. So the only one I have to worry about is replication, and then if I check for SSRS. Thanks. – Shawn Melton Aug 23 '11 at 13:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.