Last active
April 14, 2017 17:38
-
-
Save milesgratz/8c58ec247ea2191636623be79e0f86d7 to your computer and use it in GitHub Desktop.
Sample Stale Computers report (and disable)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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