The PowerShell script discussed in this post catalogs your Active Directory (AD) environment, figures out all the systems that are online, and then queries each of those systems for some common information.

One of the most common tasks many system administrators have to do is keep track of all the systems in their environment. With virtual machine (VM) sprawl being a thing now, and with multiple teams bringing up servers left and right, it's sometimes hard for a sysadmin just to keep track of what's out there.

If she doesn't have an accurate gauge of how many systems are online and the kinds of resources they are consuming, she cannot make good decisions. This is why it's important to come up with a way to stay on top of your environment. One way to do this is with PowerShell. Since PowerShell has built-in capabilities to reach out and gather all kinds of information about each system, it's the perfect product to build a tool to make this happen in your environment.

First of all, we need to find some data source where we can gather all the computer names. This can be Active Directory, a SQL database or even a CSV file. As long as the names are in some structured format, PowerShell can probably gather them somehow.

In our case, we're using Active Directory, so we'll first need to get the ActiveDirectory module. We can do this by downloading and installing Remote Server Administration Tools. After doing this, you should then have the ActiveDirectory module available to you in a PowerShell session. It's now time to get to coding.

To index computer names in Active Directory, we'll use the Get-AdComputer command and capture them all into a variable. Below we're finding all computers in AD. But with the Filter parameter and the Where-Object command, you can easily limit the list down in some ways

Now that we have all the computer names, we can then loop through all of them and test each one to ensure it's online and available. For now, we'll just ping each one, but in this step, you can also do things like test PowerShell remoting, ensure files shares are available, and so on.

We're now reporting on offline computers and have space to continue performing other things on all the available computers as well.

Next, we need to query information from these computers somehow. For this demo, I'd like to get a CSV report looking something like this:

When querying information from systems, WMI is always the best bet. However, you must figure out which class stores the information. In our case, the Win32_OperatingSystem, Win32_PhysicalMemory and Win32_Processor classes store the information. The easiest way to gather this information is via the Get-CimInstance command.

Above, I'm gathering up all the information necessary and then creating a single object containing only the information I'd like to see. This then returns only the properties I'm looking for. I can run this now and see a nicely structured output showing the computer name along with the associated information next to each.

Example output

Example output

The only thing left to do is to get this into a CSV file. With the Export-Csv command tacked on to the end of our command for each object returned, this is a cinch.

Example CSV

Example CSV

Subscribe to 4sysops newsletter!

This creates our CSV file, which contains all the information we were looking for! This example will give you a good base to build upon. This is by no means everything you can do here. Use this as a foundational tool to customize to your own needs!

  1. Some other useful attributes to consider adding,

    Which should give you the Make, Model and Serial Number.

    I have also used the method above to store this info in AD in the Computer Description Field.


  2. Kent 4 years ago

    What does the "**" do? I cannot find any documentation on it and am receiving an error "You must provide a value expression following the '*' operator."


  3. Matthew Davidson 4 years ago

    Great Article!  I also use the Win32_NetworkAdapterConfiguration in some scripts to grab the IP and MAC.  The possibilities are endless!!


  4. Mathieu 4 years ago

    if $computer is a physical one, maybe it could have more than one memory bank

    so for the psobject, it should be something like that :


Leave a reply

Please enclose code in pre tags

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


© 4sysops 2006 - 2021


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account