Located old computer accounts based on date of LastLogonTimeStamp Attribute
Pretty Easy.# import-module activedirectory$DaysInactive = 365$time = (Get-Date).Adddays(-($DaysInactive))# Get all AD computers with lastLogonTimestamp less than our timeGet-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |# Output hostname and lastLogonTimestamp into CSVselect-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv -Path "C:\Scripts\AD Computers\Old_Computers.csv" -notypeinformation
Then import the CSV file into another script to move the old computer accounts to a custom OU. I like to keep my scripts independent.
Move Computer Accounts based on Name of Account and Disable:
For those with hundreds if not thousands of old Computer Accounts, these are very powerful tools.# List to deploy$deploylist = "C:\Scripts\Sarepta\AD Computer\Old_Computers.csv"Import-Csv $deploylist -UseCulture | %{$_."Name"get-adcomputer $_."Name" | Move-ADObject -TargetPath 'ou=Old Computers,dc=avibio,dc=com'get-adcomputer $_."Name" | Disable-ADAccount -Confirm:$false}Write-Output "Loop Complete"
-Boston TechGuy