Using PowerShell and Active Directory to Create a Server or Workstation Inventory

Hi All,

In this article I will discuss how I use the Get-ADComputer cmdlet to get an inventory of computers using Active Directory and dumping it to a csv file. This can be a quick way to get a look at the machines you have, the operating systems, service pack levels etc…. You will need to use have the Active Directory Module for PowerShell installed use this cmdlet. Once you have the  Active Directory Module for PowerShell installed you can open PowerShell as Administrator and type the following to import the module:

“Import-Module ActiveDirectory”

Once you have imported the module you are ready to run the following command by changing the Searchbase to an OU in your domain as well as the path where you want to save the csv file containing the output. This example will search the “WhereIKeepMyServers” OU and every OU underneath it for computer objects and dump the specified properties to a csv.

The Command

get-adcomputer -SearchBase “OU=WhereIKeepMyservers,dc=pipe2text,dc=com” -filter * -property passwordlastset,operatingsystem,operatingsystemservicepack,CanonicalName |select name,passwordlastset,operatingsystem,operatingsystemservicepack,CanonicalName |export-csv -path c:\Servers\ServerInventory.csv

The command above it just one line. If it is not displaying this way in your browser you can cut and paste it to notepad or click the link below to see it display properly.

ADServerInventory

In the example above I use the passwordlastset, operatingsystem,  operatingsystemservicepack, and CanonicalName. There are many other properties you can use to replace these or add to the spreadsheet depending on you needs. If you want to get a list of all the properties that can be used for a computer object you can select a computer name from Active Directory and use the Get-Member cmdlet to give you all the properties available for that object. In the example below replace computername with the name of your computer object.

The Command

get-adcomputer -identity computername -property * | get-member

After you receive a list of properties you can use them to gather the information you need from you environment. I hope this helps. If you have any questions or feedback please leave a comment.

Related Links:

Using PowerShell to find Stale Computers in Active Directory

Moving Stale Computers in Active Directory to an OU using PowerShell

Using PowerShell and a Text File to Delete Multiple Active Directory Groups

Using PowerShell to export Active Directory Group Members to a CVS File

Leave a Reply

Your email address will not be published. Required fields are marked *