Read-Host input value does not complete search requirements

Jeffrey Mateo 20 Reputation points
2023-05-03T16:55:11.8233333+00:00

Hello Everyone,

I have a script that extract the uninstall string from the reg keys below

'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall*',

'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall*'

The script works fine when I specify "zoom" or other values; however, we are attempting to make this universal; however when I add read-host so it can accept a value, it creates the file but only adds /qn

Working Script

$regKeys =

'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall*',

'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall*'

$test = read-host -Prompt "Please enter software by Display Name"

$Un = Get-ItemProperty -Path $regKeys | Where-Object { $_.DisplayName -eq "Zoom Outlook Plugin"} | Select-Object -Property uninstallstring

(($un.UninstallString)-replace"/I","/X") + " /qn" | out-file "c:\RemoveOutlookplugin.bat" -Encoding ascii

Start-Sleep -Seconds 15

Invoke-Expression -Command "c:\RemoveOutlookplugin.bat"

Faulty script

$regKeys =

'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall*',

'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall*'

$test = read-host -Prompt "Please enter software by Display Name"

$Un = Get-ItemProperty -Path $regKeys | Where-Object { $_.DisplayName -eq "$test"} | Select-Object -Property uninstallstring

(($un.UninstallString)-replace"/I","/X") + " /qn" | out-file "c:\RemoveOutlookplugin.bat" -Encoding ascii

Start-Sleep -Seconds 15

Invoke-Expression -Command "c:\RemoveOutlookplugin.bat"

I hoping someone can help me use read host so that the -eq in the script actually searches for displayname and finds the correct uninstall string.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-05-03T18:55:47.4466667+00:00

    It would help to know what you're entering as the display name, and what the uninstallstring value is from the registry. It would also be helpful to know if "$Un" is a scalar value (i.e., just one item) or an array (i.e., more than one item).

    Just extracting the relevant parts of your code for a test produced the correct results:

    $test ="Zoom Outlook Plugin"
    $item = [PSCustomObject]@{displayname = "Zoom Outlook Plugin"; uninstallstring = "blah.exe /I"}
    $un = $item | Where-Object {$_.displayname -eq $test} | Select-Object -Property uninstallstring
    (($un.UninstallString)-replace"/I","/X") + " /qn"
    

    Here's your script with some changes that will figure out what's happening:

    $regKeys =  'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall*',
                'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall*'
    $test = read-host -Prompt "Please enter software by Display Name"
    Write-Host "Will try to uninstall '$test'"
    $Un = Get-ItemProperty -Path $regKeys | 
            Where-Object { $_.DisplayName -eq "$test"} | 
                Select-Object -Property uninstallstring
    if ($un -is [array]){
        Write-Host "Found $($un.count) items for removal: $($un.displayname -join ', ')"
    }
    elseif ($null -eq $un){
        Write-Host "Didn't find any items for removal"
    }
    else {
        Write-Host "Found 1 item for removal $($un.displayname)"
    }
    (($un.UninstallString)-replace"/I","/X") + " /qn" | 
        out-file "c:\RemoveOutlookplugin.bat" -Encoding ascii
    
    Start-Sleep -Seconds 15
    Invoke-Expression -Command "c:\RemoveOutlookplugin.bat"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.