PowerShell Tip of the Week: Uninstall SCCM client

Today’s short tip is about SCCM client uninstall process. One of the tasks for complete client removal is of course running ccmsetup.exe uninstall command. To do this for a bunch of servers we can use Invoke-Command.

Uninstall SCCM client

On the beginning its worth to check if ccmsetup.exe file exist on affected machine. To verify this we are going to use Test-Path command which will return “True” of “False” value:

$Server = "DC01"
Test-Path -Path "\\$Server\C$\windows\ccmsetup\ccmsetup.exe"
test path ccmsetup
test path ccmsetup

File selection can be done using System.Windows.Form feature called OpenFileDialog:

#Browsing file
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.filter = "Txt (*.txt)| *.txt"
[void]$FileBrowser.ShowDialog()
 
#Getting servers from txt file
Try
{
    $FilePath = $FileBrowser.FileName
    $Servers = Get-Content -Path $FilePath -ErrorAction Stop
}
Catch
{
    $_.Exception.Message
    Break
}

After selecting txt file script verifies if the file is empty. If not it will proceed and check if provided servers exist using System.Net.Dns class. As a verification you can check entries in log file – C:\Windows\ccmsetup\logs\ccmsetup.log

Final script:

 
#Browsing file
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.filter = "Txt (*.txt)| *.txt"
[void]$FileBrowser.ShowDialog()
 
#Getting servers from txt file
Try
{
    $FilePath = $FileBrowser.FileName
    $Servers = Get-Content -Path $FilePath -ErrorAction Stop
}
Catch
{
    $_.Exception.Message
    Break
}

#Break if file is empty 
If( $Servers.length -eq 0 ) 
{ 
    Write-Warning "Servers list is empty"
    Pause 
    Break
}

#Uninstall command 
$scriptBlock = { C:\windows\ccmsetup\ccmsetup.exe /uninstall }

#Looping each server
Foreach($Server in $Servers)
{
    $Server = $Server.Trim()
    Write-Host "Processing $Server"
 
    #Check if server exist
    $FQDN = ([System.Net.Dns]::GetHostByName(("$Server")))
 
    If(!$FQDN)
    {
        Write-Warning "$Server does not exist"
    }
    Else
    {
        #Launch ccmsetup.exe /uninstall
        Invoke-Command -Scriptblock $scriptBlock -ComputerName $Server
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.