Friday, March 16, 2018

Finding Inactive Computers With Powershell

Here is a quick script I ran at work yesterday to find inactive computers in our active directory. It checks the last time a system was logged on for a given amount of days specified by the user in the script.

Here's the code:

# Michael L. Kelley Jr.
# March 15, 2017
# InactiveComputers.ps1
# A script to find inactive computers via days not logged on

$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))

Get-ADComputer -Filter {lastlogontimestamp -lt $time} -Properties Name.OperatingSystem , lastlogintimestamp| Select Name.OperatingSystem,
@{N='lastlogontimestamp'; E={[DateTime]::FromFileTime($_. lastlogontimestamp)}}

Read-Host -Prompt "Press enter to exit"


Running this script on your AD server, will check for computers that haven't been logged on for 90 days or more. It will return the AD computer name, operating system, and time last logged on. Perfect for cleaning up active directory for systems that are no longer around. Just change the $DaysInactive variable as needed. 






No comments:

Post a Comment