Powershell: Get-OldComputers (and disable them)
-
#Gets time stamps for all computers in the domain that have NOT logged in since after specified date #See original: https://gallery.technet.microsoft.com/scriptcenter/Get-Inactive-Computer-in-54feafde import-module activedirectory ########################################################### # Modify these variables $domain = "domain.com" $DaysInactive = 90 ########################################################### $time = (Get-Date).Adddays(-($DaysInactive)) $runtime = (get-date -format g) # Get all AD computers with lastLogonTimestamp less than our time Write-Host Getting a list of systems from AD. This may take a moment. $systems = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time -and enabled -eq "true"} -Properties LastLogonTimeStamp | select-object -expandproperty Name Write-Host `nA list of inactive systems to be checked is being written to $env:HOMESHARE\Inactive_systems.txt $systems | export-csv OLD_Computer.csv -notypeinformation # out-file $env:HOMESHARE\Inactive_systems.txt # ^^^^^^^^^^ above is an alternate line to add to $systems for similar data gathering (Output hostname and lastLogonTimestamp into CSV or txt). Write-Host `nThese systems will now be tested for network connectivity.`n Foreach ($name in $systems) {Write-Host Testing $name ... #Ping Test. If PC is shut off, script will stop for the current PC in pipeline and move to the next one. if (Test-Connection -ComputerName $name -Count 1 -quiet) { # do productive stuff here Write-Host -ForegroundColor Green $name is online! ; add-content -path "$env:HOMESHARE\Inactive_systems.txt" -value "`n$name is online at $runtime" } else { # do error (no connection available) stuff here write-host -ForegroundColor Red $name is offline! ; add-content -path "$env:HOMESHARE\Disabled_systems.txt" -value "`n$name was disabled!" ; Set-ADComputer $name -Enabled $false ; Write-Host `n } } `
-
Import-Module activedirectory $target = Get-ADOrganizationalUnit -Identity "OU=Disabled Computer Accounts,OU=Space,DC=Domain,DC=com" $computers = Get-ADComputer -filter {(enabled -eq "false")} foreach ($name in $computers) { Move-ADObject $name -TargetPath $target -verbose }
Followup to above... this section would move the disabled computers to a 'disabled' OU.