none
PowerShell Function, unexpected Return Value RRS feed

  • Question

  • Hi,

    i built a function for checking if a variable registry-value exists or not. I want to return "true" if value exists and "false" if the value does not exist.
    But it seems as it does not give me back the right return value.

    Here is my Code with the function inside:

    function Exists-RegistryValue($RegistryKeyPath,$ChildItem)
        {
            $exists = Get-ItemProperty -Path $RegistryKeyPath -Name $ChildItem -ErrorAction SilentlyContinue
            If ($exists -ne $null) 
      
                {
                $return = "true"
                }
        
            elseif ($exists -eq $null)
      
            {
            $return = "false"
            }
        return $return
        }
    
     $CheckReq6 = (Exists-RegistryValue ("HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\", "NoDriveTypeAutoRun"))

    So if I request the value of my variable $CheckReq6, I always get back "true" if the Value NoDriveTypeAutoRun exists on my Registry or not...

    It does not seem to be a logic Problem but a syntax-Problem with my function... Because if I try that Code it works. Variable $return gives me the value "true" or "false" back, depending on the value NoDriveTypeAutoRun exists or not:

    $exists = Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\' -Name 'NoDriveTypeAutoRun' -ErrorAction SilentlyContinue
            If ($exists -ne $null) 
      
                {
                $return = "true"
                }
        
            elseif ($exists -eq $null)
      
            {
            $return = "false"
            }

    I do not find a solution. Please help.

    BR

    Powershell_Philipp


    • Edited by P4il1pp Tuesday, February 13, 2018 4:25 PM
    Tuesday, February 13, 2018 4:24 PM

Answers

  • Here is how to do this with PowerShell the easy way.

    function Test-RegistryValue{
        Param(
            $Path,
            $PropertyName
        )
        [bool](Get-ItemProperty -Path $Path -Name $PropertyName -ErrorAction SilentlyContinue)
    }
    
    Test-RegistryValue 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\' 'NoDriveTypeAutoRun'

    You would do better if you took time out to learn PowerShell.  You had at least three serious syntax errors in your code.

    The code you found will work but it is unnecessarily complicated for a simple test.


    \_(ツ)_/



    Wednesday, February 14, 2018 9:08 AM

All replies

  • Try simplifying your If statements, the second code block you pasted can be condensed down to:

    $exists = Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\' -Name 'NoDriveTypeAutoRun' -ErrorAction SilentlyContinue
    
    
    if ($exists){
        $return = "true"
    }else{
        $return = "false"
    }
    I would check the variables that you are passing to you Exists-RegistryValue function.

    Tuesday, February 13, 2018 4:46 PM
  • You don't need to write a function. We already have Test-Path.


    Test-Path "HKLM:\a\b\c"
    

    This will output $true if the registry subkey exists, or $false if it doesn't.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by I.T Delinquent Tuesday, February 13, 2018 5:07 PM
    Tuesday, February 13, 2018 5:01 PM
    Moderator
  • You don't need to write a function. We already have Test-Path.


    Test-Path "HKLM:\a\b\c"
    

    This will output $true if the registry subkey exists, or $false if it doesn't.


    -- Bill Stewart [Bill_Stewart]

    Okay, that is nice. But it does not solve my Problem. Because Test-Path is only for the HKLM-Path? but i want to check if there already exists a value under that path. 

    I also condensed my Code, but it did not help. My function still gives me back "True".

    Wednesday, February 14, 2018 8:42 AM
  • I found a solution over there: https://www.jonathanmedd.net/2014/02/testing-for-the-presence-of-a-registry-key-and-value.html

    That Code works.

    Wednesday, February 14, 2018 8:47 AM
  • Here is how to do this with PowerShell the easy way.

    function Test-RegistryValue{
        Param(
            $Path,
            $PropertyName
        )
        [bool](Get-ItemProperty -Path $Path -Name $PropertyName -ErrorAction SilentlyContinue)
    }
    
    Test-RegistryValue 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\' 'NoDriveTypeAutoRun'

    You would do better if you took time out to learn PowerShell.  You had at least three serious syntax errors in your code.

    The code you found will work but it is unnecessarily complicated for a simple test.


    \_(ツ)_/



    Wednesday, February 14, 2018 9:08 AM