Expanding the Active Directory Searcher and PowerShell

Posted on May 24, 2017 by Jeff Hicks in Active Directory, PowerShell with

I hope by now that you are comfortable using the Active Directory Searcher object. If you have no idea what I am talking about, take a few minutes to get caught up on the previous articles, Discovering the Active Directory Searcher with PowerShell and Finding Groups with the Active Directory Searcher and PowerShell.

 

 

Let’s expand the scope and find multiple objects with user accounts.

I have limited my search to an OU that contains my active user accounts. I have also specified the properties I want to retrieve. Once the searcher is configured, I can find all matching objects.

Finding all user objects (Image Credit: Jeff Hicks)

Finding All User Objects (Image Credit: Jeff Hicks)

Let’s look at a representative sample.

A search result (Image Credit: Jeff Hicks)

A Search Result (Image Credit: Jeff Hicks)

As we looked at before, it might be nice to clean this up and create an object to write to the pipeline.

Creating a better result object (Image Credit: Jeff Hicks)

Creating a Better Result Object (Image Credit: Jeff Hicks)

Because the result properties are treated as collection objects, I needed to expand them. I still wanted to keep properties that were actual collections, like directreports. I tested with a single object and I could modify the loop to go through everything in $all. It makes more sense to create a function.

The function is designed to take pipelined input from any search result.

Converted search result (Image Credit: Jeff Hicks)

Converted Search Result (Image Credit: Jeff Hicks)

Sponsored

Now I can process everything and work with the results as necessary.

Gridview results (Image Credit: Jeff Hicks)

Gridview Results (Image Credit: Jeff Hicks)

Here is another way to do this.

Grouping search results (Image Credit: Jeff Hicks)

Grouping Search Results (Image Credit: Jeff Hicks)

But let’s say I wanted to find all users in the Finance department. That requires some special formatting in my LDAP filter.

You need to enclose each filter part inside the (&) construct, which tells PowerShell to search for both of these things.

Filtering by department (Image Credit: Jeff Hicks)

Filtering by Department (Image Credit: Jeff Hicks)

You can keep adding filter components inside the (&).

Multiple filters (Image Credit: Jeff Hicks)

Multiple Filters (Image Credit: Jeff Hicks)

You can also use wildcards.

Filtering with wild cards (Image Credit: Jeff Hicks)

Filtering with Wildcards (Image Credit: Jeff Hicks)

Sponsored

Once you have objects in PowerShell, you can work with them any way you want. I hope you will take some time to try these things out in your own test environment. Next time, we will look at searching for groups. We also still need to address handling very large number of search results and filtering tips. I hope you will keep checking back.

Tagged with , , ,

Register for this upcoming webinar on Petri.com
Webinar: Accelerate Smart Factory ROI with Deloitte and HPE’s Digital IoT

Join HPE and Deloitte for a discussion on how to take advantage of IT and OT convergence to deliver the Factory of the Future

Tuesday, October 24, 2017
at 2 p.m. EST

Register for this upcoming webinar on Petri.com
Webinar: Accelerate Smart Factory ROI with Deloitte and HPE’s Digital IoT

Join HPE and Deloitte for a discussion on how to take advantage of IT and OT convergence to deliver the Factory of the Future

Tuesday, October 24, 2017
at 2 p.m. EST