V4.1.x - Checking If reg key is exists

Hi,
Looking for best way to check if Reg key exists and if so run command.
was wondering the if Test-path or Test-ADTRegistryValue was the way to go.
Thanks for your input

Test-path is fine for Reg keys

Test-ADTRegistryValue is for values. Hence the name.

thanks but would you have a example please, need syntax. I looked at the one in refernces but still don't get it

That's because test-path is a PowerShell command:

Further to @That-Annoying-Guy's comment, PSADT has a very good reference for the whole toolkit.
Here is the Test-ADTRegistryValue, which includes an example to get you started:

Could try something like the below as an example:

$BentleyLegionSimulator2023 = Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5998034E-D1D2-440A-94D6-CFC8C86F1BBB}"
    If($BentleyLegionSimulator2023 -eq $true){
    Write-ADTLogEntry -Message "$($adtsession.appvendor, $adtsession.appname) 2023 detected. Uninstalling."
    Start-ADTMsiProcess -Action Uninstall `
    -ProductCode "{5998034E-D1D2-440A-94D6-CFC8C86F1BBB}" `
    -LogFileName "$($adtsession.appvendor, $adtsession.appname) 2023" `
    -ArgumentList "/qn"}

    else {
    Write-ADTLogEntry -Message "No previous versions to uninstall. Moving onto pre-requisite stage"}

In v3, it was possible but according to the v4.1 docs the name of the value is a required field

Test-RegistryValue in 3.x always required a value to test, it was just that the parameter was named -Value, which was inconsistent with the other registry utilities.

[quote="Gordy_B, post:6, topic:7043"]

$BentleyLegionSimulator2023 = Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5998034E-D1D2-440A-94D6-CFC8C86F1BBB}"
    If($BentleyLegionSimulator2023 -eq $true){
    Write-ADTLogEntry -Message "$($adtsession.appvendor, $adtsession.appname) 2023 detected. Uninstalling."
    Start-ADTMsiProcess -Action Uninstall `
    -ProductCode "{5998034E-D1D2-440A-94D6-CFC8C86F1BBB}" `
    -LogFileName "$($adtsession.appvendor, $adtsession.appname) 2023" `
    -ArgumentList "/qn"}

    else {
    Write-ADTLogEntry -Message "No previous versions to uninstall. Moving onto pre-requisite stage

Thanks that worked I got it working. with your method. 

 $AutoCADLT2021 = Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{28B89EEF-4109-040C-2102-CF3F3A09B77D}"
    If($AutoCADLT2021 -eq $true){
    #Write-ADTLogEntry -Message "$($adtsession.appvendor, $adtsession.appname) 2021 detected. Uninstalling."
    Start-ADTProcess -FilePath 'C:\Windows\System32\cmd.exe' -ArgumentList "/c $($adtSession.DirFiles)\uninstall.cmd" -WindowStyle 'Hidden'
    }
    else {
    Write-ADTLogEntry -Message "No previous versions to uninstall. Moving onto pre-requisite stage"}
1 Like

You can also look at in v4 onwards the below cmdlet:

Get-ADTApplication · PSAppDeployToolkit

The problem I was facing with Bentley was, it had two uninstall strings in the registry and the cmdlet got confused. Even when I specified it either exe or msi.

Normal installs with one registry uninstall string value, that cmdlet works well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.