Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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