Updated inactive computer accounts script

Original found on Spiceworks forum. Now exports to CSV and replaces computer description with date that script is run.

# This PowerShell Command will query Active Directory and return the computer accounts which have not logged for the past
# 60 days.  You can easily change the number of days from 60 to any number of your choosing.  lastLogonDate is a Human
# Readable conversion of the lastLogonTimeStamp (as far as I am able to discern.  More details about the timestamp can
# be found at technet – http://bit.ly/YpGWXJ  –MWT, 03/12/13

# Activates the required module in PS for this script to work.
import-module activedirectory

# 30 is the number of days from today since the last logon.
$then = (Get-Date).AddDays(-30)

# Generate the CSV for those comuters affected
Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $then} | where-object {$_.DistinguishedName -notlike “*OU To Exclude*”} | Select-Object -property Name,lastLogonDate | sort Name,lastLogonDate | Export-Csv C:\SCRIPTS\Logs\Disabled_Computer_Records-$((Get-Date).ToString(‘yyyy-MM-dd’)).csv -Delimiter “;” -notype

# If you would like to Disable these computer accounts, uncomment the following line:
Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $then} | where-object {$_.DistinguishedName -notlike “*OU To Exclude*”} | Set-ADComputer -Enabled $false -Description “$($_.Description) Disabled by script on $(get-date -format yyyy-MM-dd)”

# If you would like to Remove these computer accounts, uncomment the following line:
# Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $then} | Remove-ADComputer