Remove app from user context and deploy this script via Intune

Johnny T 0 Reputation points
2024-05-17T22:50:49.1433333+00:00

I am trying to uninstall a application called MicroSIP via Microsoft Intune on all of our Windows machines but the script is says "not installed" 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 rans it looks under C:\Users for the name of the MACHINE instead of the USER when expanding %USERNAME%. I have not idea what I am doing wrong

Here is my script

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
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
5,205 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,598 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Khaled Elsayed Mohamed 1,290 Reputation points
    2024-07-07T07:50:12.3233333+00:00

    Hi Jt I see the issue in your script. The problem is that you're using environment variables in a string that's being passed to cmd.exe, but PowerShell doesn't expand these variables in that context. Let's modify the script to fix this and improve its overall structure:

    function UninstallMicroSIPFromUserProfile {
        param (
            [string]$userProfilePath
        )
    
        $MicroSIPUninstallPath = Join-Path $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
            $uninstallProcess = Start-Process -FilePath $MicroSIPUninstallPath -ArgumentList "/S" -Wait -PassThru
    
            # Check if the uninstallation was successful
            if (Test-Path -Path $MicroSIPUninstallPath) {
                Write-Output "Failed to uninstall MicroSIP from $userProfilePath. Exit code: $($uninstallProcess.ExitCode)"
            } 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 all user profiles and attempt to uninstall MicroSIP from each
    $userProfiles = Get-ChildItem "C:\Users" -Directory
    foreach ($profile in $userProfiles) {
        UninstallMicroSIPFromUserProfile $profile.FullName
    }
    

    changes and improvements:

    We're now using Join-Path to create the path to the uninstaller, which is more reliable than string concatenation. Instead of using cmd.exe, we're using Start-Process to run the uninstaller directly. This avoids issues with environment variable expansion. We've added error handling to capture the exit code of the uninstaller process. The script now checks each user profile only once, instead of the previous four repetitions. We've improved the logging to provide more informative output.

    This script should work correctly when deployed via Intune. It will iterate through all user profiles on the machine and attempt to uninstall MicroSIP from each profile where it's installed. Remember that when running this script via Intune, it will typically run in the SYSTEM context. This means it will have access to all user profiles, but won't have any specific user's environment variables loaded. The script as written doesn't rely on these variables, so it should work correctly.


    Another version:

    function UninstallMicroSIPFromUserProfile {
        param (
            [string]$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
            & "$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."
        }
    }
    
    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
    foreach ($profile in $userProfiles) {
        UninstallMicroSIPFromUserProfile -userProfilePath $profile.FullName
    }
    
    

    Explanation of Changes: Function Definition: Defined a function UninstallMicroSIPFromUserProfile to encapsulate the uninstallation logic. This function takes the user profile path as a parameter. Path Expansion: Correctly constructed the path to the Uninstall.exe using the user profile path passed to the function. Command Execution: Used the & operator to run the uninstall command correctly. Profile Loop: Loop through each user profile and call the function with the profile path. How to Use: Save the script to a .ps1 file, for example, UninstallMicroSIP.ps1. Open PowerShell with administrative privileges. Navigate to the directory where the script is saved. Run the script by executing .\UninstallMicroSIP.ps1. This script will now correctly iterate over each user profile in C:\Users, check if MicroSIP is installed, and attempt to uninstall it using the path from each user profile.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.