SCCM Clients Cleanup

I run this monthly to clean up staled sccm clients. There are 3 parts: Computer Objects no longer in AD but still in SCCM; Inactive clients ( over 30 days, defined inside SCCM collection query); Systems without SCCM agent.

# SCCM Monthly Clean Up List
# Rui Qiu
# 9/3/2021

# Setup SCCM Powershell 
Import-Module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
Set-Location XXX:

# SCCM Clients No longer in AD
$PC_NOAD = @()
$PC_SCCM = Get-CMDevice -CollectionID AMH000EB | Select-Object Name
$PC_AD = Get-ADComputer -Filter * -Properties * | Select-Object Name, Description, OperatingSystem, OperatingSystemVersion, LastLogonDate

Foreach ($PC in $PC_SCCM.Name) {
 if($PC -notin $PC_AD.Name) {$PC_NOAD+=$PC}}
$NOAD = $PC_NOAD | ForEach-Object {
    @{ 
        "Name" = $_
    }
}
New-UDTable -Data $NOAD -ShowSort -Title "Machines not in AD" -ShowSearch -Dense -Export
New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}

# Inactive SCCM Clients
$PC_Inactive = Get-CMCollectionMember -CollectionId "AMH0014C" | Select-Object Name | ForEach-Object {
    @{ 
        "Name" = $_.Name
    }
}
New-UDTable -Data $PC_Inactive -ShowSort -Title "Inactive Systems(30 Days)" -ShowSearch -Dense -Export
New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}

# Systems without SCCM Agent
$PC_NOSCCM = Get-CMCollectionMember -CollectionId "AMH0002A" | Select-Object Name | ForEach-Object {
    @{ 
        "Name" = $_.Name
    }
}
New-UDTable -Data $PC_NOSCCM -ShowSort -Title "Systems without SCCM Agent" -ShowSearch -Dense -Export

Leave a Comment