How to remove an app from user context and deploy this script via Intune

Johnny T 0 Reputation points
2024-05-17T22:37:55.2+00:00

Hey guys,

So I cant figure out why my script is not working. I am trying to uninstall a application called MicroSIP (website: microsip.org) via Microsoft Intune on all of our Windows machines but the script is says "not installed" or "error" every time I run it. I have tied multiple scripts but no luck. I did find my issue. So here it is. Every time the script runs it looks under C:\Users for the name of the MACHINE instead of the USER when expanding %USERNAME%. Can someone please help me fix this? Here is my script and screen screenshot of Intune Log.

Function to uninstall MicroSIP from a specific user profile

function UninstallMicroSIPFromUserProfile($userProfilePath) {

$MicroSIPUninstallPath = "$userProfilePath\AppData\Local\MicroSIP\Uninstall.exe"

Write-Output $MicroSIPUninstallPath

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"

    # 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."

}

}

if (!(Test-Path -path "C:\Users")) {

Write-Output "Users directory does not exist on this drive. Current location: "

Write-Output $MyInvocation.MyCommand.Path

Exit

}

Get the list of user profiles on the machine

$userProfiles = Get-ChildItem "C:\Users" -Directory

Loop through each user profile and uninstall MicroSIP

foreach ($profile in $userProfiles) {

UninstallMicroSIPFromUserProfile $profile.FullName

UninstallMicroSIPFromUserProfile $profile.FullName

UninstallMicroSIPFromUserProfile $profile.FullName

UninstallMicroSIPFromUserProfile $profile.FullName

}

User's image

Microsoft Intune Configuration
Microsoft Intune Configuration
Microsoft Intune: A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.Configuration: The process of arranging or setting up computer systems, hardware, or software.
1,756 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,185 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Gowtham CP 2,215 Reputation points
    2024-05-18T03:54:49.9033333+00:00

    Hello Johnny T ,

    Thank you for reaching out on Microsoft Q&A.

    I have updated the script to correctly reference the user profile paths using a parameter within the function, ensuring the correct user-specific paths are targeted during the uninstallation process. I have also removed Redundant function calls to improve the efficiency.

    
    # Function to uninstall MicroSIP from a specific user profile
    
    function UninstallMicroSIPFromUserProfile {
    
        param (
    
            [string]$userProfilePath
    
        )
    
        $MicroSIPUninstallPath = "$userProfilePath\AppData\Local\MicroSIP\Uninstall.exe"
    
        Write-Output "Checking path: $MicroSIPUninstallPath"
    
        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
    
            & $MicroSIPUninstallPath /S
    
            # 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."
    
        }
    
    }
    
    # Check if the Users directory exists
    
    if (!(Test-Path -Path "C:\Users")) {
    
        Write-Output "Users directory does not exist on this drive. Current location: $($MyInvocation.MyCommand.Path)"
    
        Exit
    
    }
    
    # Get the list of user profiles on the machine
    
    $userProfiles = Get-ChildItem "C:\Users" -Directory
    
    # Loop through each user profile and uninstall MicroSIP
    
    foreach ($profile in $userProfiles) {
    
        UninstallMicroSIPFromUserProfile $profile.FullName
    
    }
    
    

    If you found this solution helpful, consider accepting it.