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.