Search for Windows 7 Systems, Disable, Move and Remove from SCCM

This script will search AD for any computer objects that match the OperatingSystem value of Windows 7. It will then disable those systems and move them to a container called “To Be Removed” at the root of the domain.

The second part imports your SCCM module, mounts the drive and then removes those same objects from SCCM.

# To View Windows 7 Systems
Get-ADComputer -Filter {OperatingSystem -Like "*Windows 7*"} -Property * | Format-Table Name,OperatingSystem,Description,Enabled,DistinguishedName -Wrap -Auto

# Gather Windows 7 Systems
$win7systems = Get-ADComputer -Filter {OperatingSystem -Like "*Windows 7*"} -Property *

# Disable and Move to Disabled Computers
foreach ($system in $win7systems) {Set-ADComputer $system.Name -Enabled $false; Move-ADObject $system.DistinguishedName -TargetPath "OU=To Be Removed,DC=DOMAIN,DC=DOMAIN,DC=TLD"}

# Remove from SCCM
# Site configuration
$SiteCode = "SITECODE" # Site code 
$ProviderMachineName = "SCCM SERVER ADDRESS" # SMS Provider machine name

# Import the ConfigurationManager.psd1 module 
if((Get-Module ConfigurationManager) -eq $null) {Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName}

# Set the current location to be the site code.
Set-Location "$($SiteCode):\"

# Remove SCCM Objects
foreach ($system in $win7systems) {Remove-CMDevice $system.Name -Force -ErrorAction Ignore}

Leave a comment

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