Skip to content

Instantly share code, notes, and snippets.

@milesgratz
Last active April 14, 2017 17:38
Show Gist options
  • Save milesgratz/8c58ec247ea2191636623be79e0f86d7 to your computer and use it in GitHub Desktop.
Save milesgratz/8c58ec247ea2191636623be79e0f86d7 to your computer and use it in GitHub Desktop.
Sample Stale Computers report (and disable)
# Search for stale computers
$StaleComputers = Get-ADComputer -Filter {
Enabled -eq $true -and OperatingSystem -like "*Windows*" -and OperatingSystem -notlike "*Server*"
} -Properties LastLogonDate,Description,Location,OperatingSystem,CanonicalName |`
Where-Object { ((Get-Date) - ($_.LastLogonDate)).TotalDays -gt 45 }
# Export results to C:\temp
$StaleComputers | Export-Csv C:\temp\Stale-Computers.csv -NoTypeInformation
# Uncomment to disable
# $StaleComputers | Disable-ADAccount
# I want to loop through a "local copy" of the AD objects
$StaleComputers = Import-Csv C:\temp\Stale-Computers.csv
$StaleComputers | ForEach-Object {
$ADObject = Get-ADComputer $_.DistinguishedName
$ADObject | Disable-ADObject -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment