Skip to content

Instantly share code, notes, and snippets.

@c3rberus
Created January 9, 2022 05:08
Show Gist options
  • Save c3rberus/20bc3c9eff76cb915b2a9c4b88ec2a6e to your computer and use it in GitHub Desktop.
Save c3rberus/20bc3c9eff76cb915b2a9c4b88ec2a6e to your computer and use it in GitHub Desktop.
# Find computers that have not logged in for over 90 days
# Skip ConfigMgr Excluded OU
$DaysInactive = 90
$time = (Get-Date).Adddays(- ($DaysInactive))
$ComputerList = Get-ADComputer -Filter { LastLogonTimeStamp -lt $time -and enabled -eq $true } -SearchBase "OU=Computers,DC=CORP,DC=MIRZADEDIC,DC=CA" -Properties Name, LastLogonTimeStamp, DistinguishedName | Where { $_.DistinguishedName -notmatch "OU=ConfigMgr Excluded,OU=Computers,DC=CORP,DC=MIRZADEDIC,DC=ca" }
# Disable computer
# Move to ConfigMgr Excluded OU
ForEach ($Computer in $ComputerList)
{
Write-Output "Disabled: $($Computer.Name)"
Set-ADComputer $Computer.Name -Enabled $false -Verbose
Move-ADObject -Identity $Computer.DistinguishedName -TargetPath "OU=ConfigMgr Excluded,OU=Computers,DC=CORP,DC=MIRZADEDIC,DC=CA" -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment