powershell_logo

I had a question in our User Group during Powershell Script Club.   Would he be able to use Powershell to show a list of all computers in Active Directory?

Well I took a shot at it tonight.

With Quest Active Roles installed I ran a

GET-QADCOMPUTER | EXPORT-CSV C:\Computers.csv

and examined the Headers from the CSV file in Excel to see what data was available.  I spotted about four date columns but it appears there was some duplication but the useful ones seem to be “CreationDate” and “ModificationDate”

CreationDate seems simply enough to be the Date the object got Created in Active Directory

ModificationDate seems to be the last time the Object was accessed in A/D by the computer for any reason.  It also seems to contain a date field that is update somewhat quasi regularly by a live system.

So running the process is typical date compare stuff.  Store the current date and Compare against the date

 

$COMPAREDATE=GET-DATE

GET-QADCOMPUTER | WHERE { $_.ModificationDate.CompareTo( $COMPAREDATE.AddDays(-90) ) –eq $TRUE }

 

Should give us a nice simple list of computers that are ACTIVE within the network in the last 90 days.

Now mind you this is not a precise science.  I wouldn’t just go ACTIVELY purging objects with this, but at least it can help you see a list of Computer Objects you can probably purge in the A/D

 

And of course if your Pipe that into an EXPORT-CSV you can get a nice list in Excel to see and work with.  

 

$COMPAREDATE=GET-DATE

GET-QADCOMPUTER | WHERE { $_.ModificationDate.CompareTo( $COMPAREDATE.AddDays(-90) ) –eq $TRUE } | EXPORT-CSV C:\IDLECOMPUTERS.CSV

 

Keep in mind as well, if you have Virtual Machines that don’t normally get powered up and SIT for 90 days, they would also fall into this list.  Thus why I said use it as a reference list. DON’T and I repeat DON’T just pipe this into a Remove-Object without a –whatif unless you’re in a Test Domain.

Or unless you’re looking to get fired really quick.

 

Powershell

It just makes life EASIER!

 

Sean
The Energized Tech

Advertisements