MECM: uninstall MECM client from a list using PowerShell

Just another Tech site

MECM: uninstall MECM client from a list using PowerShell

Trying to cleanly clean up MECM client installation

# Path to list of computers
$ComputerList = Get-Content "C:\Scripts\Computers.txt"

# Log file
$LogFile = "C:\Scripts\SCCM_Removal_Log.txt"
"=== SCCM/MECM Client Removal Log - $(Get-Date) ===" | Out-File $LogFile

# Max concurrent jobs
$MaxJobs = 20

# Script block for removal
$RemoveSCCM = {
param($ComputerName)

try {
Write-Host "[$ComputerName] Starting removal..." -ForegroundColor Cyan

# Stop SCCM-related services
$services = @("CcmExec", "smstsmgr", "ccmsetup", "CmRcService")
foreach ($svc in $services) {
if (Get-Service -Name $svc -ErrorAction SilentlyContinue) {
Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
Set-Service -Name $svc -StartupType Disabled -ErrorAction SilentlyContinue
}
}

# Kill SCCM-related processes
$processes = @("ccmexec", "ccmsetup", "smsswd", "tsmanager", "CmRcService")
foreach ($proc in $processes) {
Get-Process -Name $proc -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
}

# Uninstall SCCM client
$ccmSetupPath = "$env:windir\ccmsetup\ccmsetup.exe"
if (Test-Path $ccmSetupPath) {
& $ccmSetupPath /uninstall
Start-Sleep -Seconds 20
}

# Remove SCCM folders
$folders = @(
"$env:windir\ccm",
"$env:windir\ccmsetup",
"$env:windir\ccmcache",
"$env:ProgramFiles\CCM",
"$env:ProgramFiles\Microsoft Configuration Manager",
"$env:ProgramData\Microsoft\Configuration Manager"
)
foreach ($folder in $folders) {
if (Test-Path $folder) {
Remove-Item -Path $folder -Recurse -Force -ErrorAction SilentlyContinue
}
}

# Clean registry keys
$regPaths = @(
"HKLM:\SOFTWARE\Microsoft\CCM",
"HKLM:\SOFTWARE\Microsoft\CCMSetup",
"HKLM:\SOFTWARE\Microsoft\SMS",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\CCM",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\SMS"
)
foreach ($reg in $regPaths) {
if (Test-Path $reg) {
Remove-Item -Path $reg -Recurse -Force -ErrorAction SilentlyContinue
}
}

Write-Host "[$ComputerName] Removal completed." -ForegroundColor Green
return "SUCCESS: $ComputerName"
}
catch {
Write-Host "[$ComputerName] ERROR: $_" -ForegroundColor Red
return "FAILED: $ComputerName - $_"
}
}

# Launch jobs in batches
foreach ($Computer in $ComputerList) {
while ((Get-Job -State Running).Count -ge $MaxJobs) {
Start-Sleep -Seconds 2
}

Start-Job -Name $Computer -ScriptBlock {
param($Comp, $Script)
Invoke-Command -ComputerName $Comp -ScriptBlock $Script -ArgumentList $Comp -ErrorAction Stop
} -ArgumentList $Computer, $RemoveSCCM | Out-Null
}

# Wait for all jobs to finish
Write-Host "Waiting for all jobs to complete..." -ForegroundColor Yellow
while (Get-Job -State Running) {
Start-Sleep -Seconds 5
}

# Collect results
$Results = Receive-Job -State Completed
$Results | Out-File -Append $LogFile

# Cleanup jobs
Remove-Job -State Completed -Force

Write-Host "=== All tasks completed. Log saved to $LogFile ===" -ForegroundColor Yellow