Script to uninstall non Win32 program for all profile users

I am trying to uninstall a non Win32 app with the following code:

$appName = "my_non_Win32_app"
$user = $env:USERNAME

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location -Path $scriptPath

Write-Output "Script Initialization..."

function Get-App {

    param(
        [Parameter(Mandatory=$true)]
        [string]$Name
        )

    # Get application GUID

    $app = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* `
    -ErrorAction SilentlyContinue | `
    Where-Object {$_.DisplayName -like "$Name"}) | `

    Select DisplayName, PSChildName, DisplayVersion, UninstallString

    $app += (Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* `
    -ErrorAction SilentlyContinue | `
    Where-Object {$_.DisplayName -like "$Name"}) | `

    Select DisplayName, PSChildName, DisplayVersion, UninstallString

    return $app
}

$GUID = Get-App -Name "$($appName)"

If ($GUID -ne $Null) {

    foreach ($app in $GUID)  {

        $guid = $app.PSChildName

        Start-Process msiexec.exe -ArgumentList `
        "/x $guid /passive /norestart" -Wait
         
        $appGUID = Get-App -Name $app.DisplayName

        If (!$GUID) {
           Exit 0
        }

        else {
            Start-Process $($app.UninstallString) -ArgumentList `
            "/S" -Wait

            $GUID = Get-App -Name $app.DisplayName

            If (!$GUID) {

                Write-Output "App `"$($app.DisplayName)`" has been uninstalled successfully!"

                Exit 0

            } else {
            
                Write-Output "App `"$($app.DisplayName)`" FAILED to uninstall using msiexec and Uninstall string methods"

                Exit 1
            }
        }
    }
}

else

{
    Write-Output "No `"$appName`" app found!"
    Exit 0
}

It works only when I am logged in as administrator. I want to make it run regardless of user rights.

Giannis,
Welcome to the forum. :wave:t3:

If it is installed for “All Users” you need to run it elevated. There’s no way around it. :man_shrugging:t3: