How can I find all computer accounts in my Active Directory domain that have been inactive for x days using PowerShell?
Note that I do actually know how to do this. This is a self-answered question just to get the knowledge out there. If anyone else has a better way, feel free to post it!
This would give you all computer accounts that have no activity for the last 365 Days.
This would sort it for you by lastlogondate.
This would give you disabled computer accounts.
Computers change their account password every 30 days by default. If a computer hasn't changed its password in an extended period of time, it means that they are no longer connected to the network.
This PowerShell script will output 2 text files. One is for disabled computers, one is for orphaned computer account objects. You must have the Active Directory PowerShell module installed.
In this example, I exclude an "Encrypted Laptops" OU, since they're mobile laptops that are disconnected for extended periods of time. You can remove that section if you don't have a similar setup
A million thank you's! I wanted to add my tweak to this. I needed to find only servers that have either been disabled or not disabled and not in production. This is what I came up with and it seemed to work.
I know the OP clearly asked for PowerShell but if you don't like it, don't have it, and don't want to learn yet another Microsoft syntax then the following Python snippet will give you a date in the correct format to use with an LDAP query.
Which could then be used as follows to find all Windows computers which haven't changed their passwords in the last 90 days.
You probably only need 30 as the default period for Windows machines to change their password is 30 days but 90 seems safer in case you forgot about that PC which is sat underneath Bob's desk and never gets turned on.
EDIT: Oh also I've omitted time-zone support in this which probably doesn't matter in this use case but may in others.