You are putting the full path to the executable into the MicroSIPUninstallPath variable, so there no need to reference %username%. There is also no need to call cmd.exe as Powershell can just run the program directly.
The main problem that you have is how the application's installer functions. If it installs on a per user basis, then it will likely save registry keys into HKEY_CURRENT_USER. If Intune is running your script as the SYSTEM account, then uninstall.exe is not going to find any registry entries and is going to fail. If it just looks to .INI or other config files, then that should work, but when you launch the uninstall.exe you may need to set the working directory to the directory where those files are located.
In addition to the "/S" switch, you should investigate the switch that generates a log file. That way if uninstall.exe does not work, you can look to its log file to see what it did not like.
See the comments in the script and select option 1 or 2.
function UninstallMicroSIPFromUserProfile($userProfilePath) {
$MicroSIPUninstallPath = "$userProfilePath\AppData\Local\MicroSIP\Uninstall.exe"
if (Test-Path -path $MicroSIPUninstallPath) {
Write-Output "Uninstalling MicroSIP from $userProfilePath..."
# Stop any running instances of MicroSIP to avoid conflicts
Stop-Process -Name MicroSIP -Force -ErrorAction SilentlyContinue
# Uninstall MicroSIP
#cmd.exe /c ""%SYSTEMDRIVE%\Users\%USERNAME%\Appdata\Local\MicroSIP\Uninstall.exe" /S"
Start-Process -FilePath $MicroSIPUninstallPath -ArgumentList "/s" -Wait
# Check if the uninstallation was successful
if (Test-Path -path $MicroSIPUninstallPath) {
Write-Output "Failed to uninstall MicroSIP from $userProfilePath. Please try again or manually remove it."
} else {
Write-Output "MicroSIP successfully uninstalled from $userProfilePath."
}
} else {
Write-Output "MicroSIP is not installed in $userProfilePath."
}
}
# Choose Option 1 or 2
# Option 1
# If Uninstall.exe does not need to reference HKEY_CURRENT_USER,
# and you have configured Intune to execute this script as the SYSTEM account,
# then use this foreach loop to uninstall the app for each user.
#Get the list of user profiles on the machine
if (!(Test-Path -path "C:\Users")) {
Write-Output "Users directory does not exist on this drive. Current location: "
Write-Output $MyInvocation.MyCommand.Path
Exit
}
$userProfiles = Get-ChildItem "C:\Users" -Directory
#Loop through each user profile and uninstall MicroSIP
foreach ($profile in $userProfiles) {
Write-Output ""
"Checking {0}" -f $profile.FullName | Write-Output
UninstallMicroSIPFromUserProfile $profile.FullName
}
# Option 2
# If Uninstall.exe does reference HKEY_CURRENT_USER,
# and you have configured Intune to execute this script in the context of the user's account,
# then use this to uninstall the app
Write-Output "Running as the user, not the SYSTEM account."
"Checking {0}" -f $env:USERPROFILE | Write-Output
UninstallMicroSIPFromUserProfile $env:USERPROFILE