question

Junior8805-9428 avatar image
0 Votes"
Junior8805-9428 asked IanXue-MSFT commented

Powershell Registry check fails

Hey there coders!

First time on here, and only been coding in PS for a week, so be gentle :).

I have made a rather long script that does the following:

  • Ask for a installed Product Name

  • Ask for the publisher (failsafe, will remove later)

  • Scan Uninstall reg keys for matches of publisher and product name using wildcards

  • if matches found, prompt for each match if removal is to happen

  • If yes, then kick of MSIEXEC for the product (I have removed this step, and replaced it with a "writehost" of the msiexec for now, while testing)

  • Once done, check the reg key to see if it still exists (if still there, removal failed, if gone, removal worked)

  • Then moves on to next product, and so forth.

All my code works as expected except for the Reg check part, it always returns as "false", but the key has not been removed as I have not performed any uninstall in testing phase, so the key still exists!

I have temporarily chucked code in to spit out the variables full path, and the value of the "test-path" to the console to see what it is doing.

Example Output:

 Found: Intel(R) Wireless Bluetooth(R) : {00000010-0200-1033-84C8-B8D95FA3C8C3}
 Are you sure you want to remove Intel(R) Wireless Bluetooth(R)?: y
 Removing...
 msiexec.exe /x {00000010-0200-1033-84C8-B8D95FA3C8C3} /qn /norestart
 False
 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{00000010-0200-1033-84C8-B8D95FA3C8C3}
 Removed 'Intel(R) Wireless Bluetooth(R)' successfully



This is the code Snippet where this part resides:

 $keys = Get-ChildItem -path $UninstKey | 
     Where-Object { $_.PSChildName -match '^{\w{8}-(\w{4}-){3}\w{12}}$' -and ($_.GetValue('DisplayName') -like "*$prodname*") -and ($_.GetValue('Publisher') -like "*$Vendorname*") } |
     Select-Object @{n='GUID';e={$_.PSChildName}}, @{n='Vendor';e={$_.GetValue('Publisher')}}, @{n='Product'; e={$_.GetValue('DisplayName')}}, @{n='Name'; e={$_.Name}}
 if ($keys.count -eq 0) {
     Write-Host "Nothing found!"
     Exit
 } Else {
     Write-Host "Products found!"
 }
 foreach ($key in $keys) {
     Write-Host `n"Found: $($key.Product) : $($key.GUID)"
     $confirmation = Read-Host "Are you sure you want to remove $($key.Product)?"
     if ($confirmation -eq 'y') {
         $MSIargs = @(
         "/x"
         "$($key.GUID)"
         "/qn"
         "/norestart"
         )
         Write-Host "Removing..."
         Write-host "msiexec.exe" $MSIargs     #Start-Process "msiexec.exe" -ArgumentList $MSIargs -Wait } -NoNewWindow #The Nuke Payload
         Write-Host (Test-Path $key.Name)
         Write-Host $key.Name
         if (Test-Path $key.Name) {    #$testremove
             Write-Host "Failed to remove: '"$($key.Product)"'"
         }
         else {Write-Host "Removed" "'$($key.Product)'" "successfully"}


As you can see with the Write-Host, the value of "$key.name" is the correct registry path, but returns a "false"?
As mentioned, the rest of the script works fine without errors and everything else works as intended, I literally just need the value to read true when the registry exists.

As mentioned, I can navigate to this location in regedit without issues.

I am certain it is something super simple I have looked over and I would greatly appreciate your help on this!
Please let me know if you need any more information!







windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT commented

Hi,

The path should be HKLM:\SOFTWARE\..., not HKEY_LOCAL_MACHINE\SOFTWARE\...

 Write-Host (Test-Path ($key.Name -replace "HKEY_LOCAL_MACHINE","HKLM:"))

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey Ian,

This is perfect!
I did not realise it had to be shortened, nor did I know about the replace command.
Legend!

Thanks,
Jai

0 Votes 0 ·

"HKLM:" is a drive provided by the registry provider that makes it easier to access the registry data. You can refer to this for more details.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_providers

0 Votes 0 ·