How to combine 2 "get-aduser" commands into one report

Hello.  I have 2 pieces of information that I need to combine into one report.
First: list members of GROUPA and GROUPB
Second: list users in OU=THISOU.

As you can see, everything from "Select-Object" and down is the same for both.  I believe I just need to find how to combine the two Get-ADUser commands.  Thanks in Advance.

$UserExport =  Get-ADUser -Filter * -Properties memberOf | `
Where-Object {
    $_.memberof.contains('CN=GROUPA,OU=Groups,OU=HERE,OU=THERE,DC=ACME,DC=com') -or
    $_.memberof.contains('CN=GROUPB,OU=Groups,OU=HERE,OU=THERE,DC=ACME,DC=com')
    } |
    Select-Object -Property @{n="ColumnA"; e={"CUSTOM COLUMN"}}, SamAccountName, Surname, GivenName |
    sort SamAccountName |
  ConvertTo-CSV -NoTypeInformation |
  Select-object -Skip 1 |
  Set-Content -Path "C:\Scripts\Output\THESEGROUPS.csv"

_______________


$UserExport =  Get-ADUser -Filter * -SearchBase "OU=THISOU,OU=HERE,DC=ACME,DC=com" |
  Select-Object -Property @{n="ColumnA"; e={"CUSTOM COLUMN"}}, SamAccountName, Surname, GivenName |
  sort SamAccountName |
  ConvertTo-CSV -NoTypeInformation |
  Select-object -Skip 1 |
  Set-Content -Path "C:\Scripts\Output\THISOU.csv"
deniecegreenwoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian ScholerCommented:
Your first call to Get-ADUser is with -Filter * and no search base, so it's searching every user anyway. That means the second call is already a subset of the first.

Since you're piping to where-object to filter your results on the first call, the key is to just include a condition in the Where-Object block to include accounts that fall within the OU you want.

I believe an easy way to do that is to look at the distinguished name of the user, and do a -like compare to match the OU you want to the end of the DN, since a typical user name might be "CN=Some Guy,OU=THISOU,OU=HERE,DC=ACME,DC=com":

$UserExport =  Get-ADUser -Filter * -Properties memberOf | `
Where-Object {
    $_.memberof.contains('CN=GROUPA,OU=Groups,OU=HERE,OU=THERE,DC=ACME,DC=com') -or
    $_.memberof.contains('CN=GROUPB,OU=Groups,OU=HERE,OU=THERE,DC=ACME,DC=com') -or
    $_.DistinguishedName -ilike '*,OU=THISOU,OU=HERE,DC=ACME,DC=com'
    } |
    Select-Object -Property @{n="ColumnA"; e={"CUSTOM COLUMN"}}, SamAccountName, Surname, GivenName |
    sort SamAccountName |
  ConvertTo-CSV -NoTypeInformation |
  Select-object -Skip 1 |
  Set-Content -Path "C:\Scripts\Output\THESEGROUPS.csv"

Open in new window


Note that -ilike forces the case-insensitive version of -like. * is the wildcard character.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deniecegreenwoodAuthor Commented:
Outstanding!  Thank you so very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.