If you work with Active Directory (AD), Get-ADComputer is a PowerShell cmdlet you will use at some point to search in AD. And if you're like me, you will use it multiple times per day.

Mike Kanakos

Mike is a Windows IT pro located in the Research Triangle Park area of North Carolina with 13+ years of experience as an admin and 20 years in the field. He specializes in Active Directory, Azure AD, Group Policy, and automation via PowerShell. You can follow Mike's blog at networkadm.in or on Twitter at @MikeKanakos.

Get-ADComputer is basically a search cmdlet. It might seem easier to use the GUI for searching rather than the command line, but that's not entirely true. Yes, searching with Active Directory Users and Computers or Active Directory Administrative Center can be fast, but trying to perform complex searches with multiple criteria is difficult at best and usually not doable at all. Once you get used to the simple cmdlet syntax of Get-ADComputer, you quickly realize how fast you can pull complex sets of information using PowerShell.

So, with that said, let's look at some interesting ways we can get creative with the Get-ADComputer cmdlet. But first, let's construct a simple search:

We use the -filter parameter to find various data in Active Directory. One example of a simple search would be if we were to look for a computer that contains the text NSK somewhere in the name. That search would look like this:

This search returns all computers that contain NSK in their names and outputs only the name of each computer.

Searching for computers by name

Searching for computers by name

Let's say we wanted to search for domain controllers (DCs). How could we accomplish that? We could do this probably five or six different ways with various AD cmdlets. Since we're working with the Get-ADComputer cmdlet today, let's search by name. That would be an easy task if your naming convention included the letters dc or something similar in the name. To do that, we would type:

This search returns all computers that contain dc in the name. Unfortunately, this search also returns any computers that are not domain controllers.

Searching for computers with DC in the name

Searching for computers with DC in the name

So how else could we find DCs using Get-ADComputer? Another way would be to get a little creative and search for computers with DC in the name that also run a server OS. We could do this by querying the operatingsystem property and filtering on the word server. That search would build on our previous example and would look like this:

This search looks at two fields in AD (name and operatingsystem) and returns only values that match both criteria.

Search using two Active Directory parameters

Search using two Active Directory parameters

Active Directory computers contain a property called ManagedBy that allows you optionally to assign a user or group to a computer. I have used this field in the past to assign a person responsible for maintaining patches for a computer. The field could serve many different uses such as keeping track of who the owner of an application is. Let's use what we just saw in the previous example to find servers owned by Michael Kanakos.

This search is going to filter for computers with the word server in the operatingsystem name and the managedby property that equals the name Michael_Kanakos. It will display only the name and managedby fields and lastly, it will output the data in a table format.

Search using the managedby field in AD

Search using the managedby field in AD

Maybe you need a report of all servers and assigned owners. This search below will dump out a list of all servers and the managedby field for each server. In this search, I used the sort command to sort by assigned owner.

Now we're going to create a list of servers and assigned owners and save the output as a CSV file, which is super easy to open in Excel and format/sort as you wish.

Notice we used the operatingsystem field to filter the results, but we don't display the operatingsystem field in the output. You could display it if you needed it or not. That's the beauty of doing these searches in PowerShell; you get exactly the output you want.

How about if you needed to find all computers created in the last 40 days?

We can do that, but it requires a little more work. We start off the same as before using a filter to find only servers. Then we pass the results to the Where-Object cmdlet and use the WhenCreated property to filter for only servers created in the last 40 days (FYI -lt stands for than than).

Computers created in the last 40 days

Computers created in the last 40 days

We can turn this into a report for management by using what we learned just a few examples earlier. We run the same search, but instead of the results displaying in a table on the screen, we send the output to a CSV file for importing into Excel.

The idea with these search examples is simply to highlight that you can quickly build fully customized, repeatable searches from the command prompt that will give you the exact data you are looking for without much work. The GUI tools included in Active Directory are useful, but they will never be able to search with the same precision as PowerShell. The speed and flexibility enabling you to do your job greatly offset the small upfront effort required to learn the syntax.

Join the 4sysops PowerShell group!

6+

Users who have LIKED this post:

  • avatar
  • avatar
Share
8 Comments
  1. Scott W 2 years ago

    Great article; helpful content!

    1+

  2. Dustin Thompson 2 years ago

    Good article. I Look forward to more.

    0

    • Author
      Mike Kanakos 2 years ago

      Glad you liked it and thanks for taking the time to leave a note of thanks. If there's a topic that you would like to read about, let me know and I'll see what I can come up with.

      0

  3. PowerMe! 2 years ago

    Very Nice and informative article. Get-ADComputer is one of my favorites. I thought the following would be of interest.

    I normally use a variable to store the data then filter it as desired, saves time.
    I also found Get-..... | Out-GridView a nice way to output the filtered data. It gives a windows were one can further filter data. My favorite part is the ability to Control+A & Control+C from that window and paste into Excel!
    An equally useful command-let is get-ciminstance . I was amazed how it pulled the data.

    4+

    Users who have LIKED this comment:

    • avatar
    • avatar
    • Author
      Mike Kanakos 2 years ago

      I use out-gridview all the time as well! It's one of my fav quickie output methods!

      Thanks for the CTRL-A & CTRL-V tips; either I never thought to try that or I failed miserably many moons ago and never tried since then.

      Never too old to learn new tricks!

      1+

      Users who have LIKED this comment:

      • avatar
  4. PowerMe! 1 year ago

    @mike Kanakos  "Never too old to learn new tricks!" I am trying to figure out getting used to the UI.

    You are right, every day I am learning something new in PowerShell  and I get so excited that I find someone around me to tell it (that's why people are afraid of me these days!).
    Sure try the CTRL-A , CTRL-C  (sorry about the typo) followed by CTRL-V on Excel. Recently I imported 2000 accounts to excel this way from my get-mailbox in exchange
    The Excel Data Query by itself is cool too ( I read it somewhere in the forums here), although I could not make it work for Exchange will try again.

    0

  5. Ryan Ramnath 8 months ago

    Hi, I am new to powershell, in my new role at work I need to start learning how to use it. This was very helpful and made powershell seem not quite so intimidating - Thanks so much!

    0

    • Author
      Mike Kanakos 8 months ago

      Hi Ryan,

      Thanks for taking the time to leave a comment. I am glad my article was useful to you. Give me an idea of another concept in PowerShell you are struggling with and I'll try to write an article that focuses on that topic.

      0

Leave a reply

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

*

© 4sysops 2006 - 2019

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account