pedaleo
pedaleo

Reputation: 411

Test-path returning false when it should be true

I'm wondering why test-path is returning true and false with two statements, can anyone explain or suggest why?

$app2find = "Easeus"

### search ###
$appSearch = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match $app2find } |
            Select-Object -Property DisplayName, UninstallString

### search results ###
if (!$appSearch) { "No apps named like $app2find were found" }

### uninstall ###
ForEach ($app in $appSearch) {

    If ($app.UninstallString) {

        Test-Path $app.UninstallString
        Test-Path "C:\Program Files (x86)\EaseUS\EaseUS Partition Master 10.5\unins000.exe"

        #& cmd /c $($app.UninstallString) /silent
    }
}

Output:

False
True

Desired output:

True
True

many thanks

*EDIT

$app.UninstallString is a value in the registry that provides the way to uninstall a specific app. In this case exactly prints:

"C:\Program Files (x86)\EaseUS\EaseUS Partition Master 10.5\unins000.exe"

Upvotes: 1

Views: 1887

Answers (1)

Matt
Matt

Reputation: 46730

I think Etan has it right based on what you have shown us. Only thing we can figure is that $app.UninstallString does not contain an absolute path like you believe it does. Best guess is that the string is already quoted in the registry. Test-Path does not resolve strings with outer quotes.

Consider the following examples

PS Z:\> test-path "c:\temp"
True

PS Z:\> test-path "'c:\temp'"
False

PS Z:\> test-path "'c:\temp'".Trim("'")
True 

Perhaps you just need to trim quotes?

Test-Path $app.UninstallString.Trim("'`"")

That should remove trailing and leading single and double quotes.

Upvotes: 3

Related Questions