# Multiple domain search with user

**URL:** <https://forums.powershell.org/t/multiple-domain-search-with-user/16295>\
**Category:** PowerShell Help\
**Created:** [April 19, 2021, 1:59pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295 "2021-04-19T13:59:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 19, 2021, 1:59pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/1 "2021-04-19T13:59:41Z")

</div>

i am able to run get-aduser cmdlet but when i try with one user it only gives for current domain how i can check if user has another domain ?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 19, 2021, 3:09pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/2 "2021-04-19T15:09:55Z")

</div>

> [@nik3387](#):
>
> if user has another domain ?

user has another domain ? Can you clarify it little more.  
Are you trying to get user from another domain ?

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 19, 2021, 4:17pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/3 "2021-04-19T16:17:50Z")

</div>

meand user has account in multiple domain like

asia, emea, nawest, naeast are domains

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 20, 2021, 4:10am UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/4 "2021-04-20T04:10:01Z")

</div>

That means, those are different user accounts in different domains.  
Can you share the code you are using ? You can use `-Server` parameter of `Get-AdUser` cmdlet where you can specify another domain, you will get the result if the user running this cmdlet has permissions.

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 20, 2021, 11:10am UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/5 "2021-04-20T11:10:24Z")

</div>

i was able to get user search, now my issue is when i search for memberof for one user it gives me list of all groups which user is part list looks like

CN=GNAEAST-VCTX-TELE-TSYS2-2-V1,OU=Virtual,OU=CoreDep,OU=Deployments,OU=Groups,DC=NAEAST,DC=AD,

However i want to just get result like GNAEAST-VCTX-TELE-TSYS2-2-V1, not sure how to get this

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 20, 2021, 4:57pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/6 "2021-04-20T16:57:06Z")

</div>

ok. So you just want to get only the username from the DistinguishedName.

you can easily use split or regex here. Split will be an easy one to understand.

```auto
# if $r is the variable you have the DistinguishedName value
$CNName = ($r -split ',')[0] # the first one which is CN=GNAEAST-VCTX-TELE-TSYS2-2-V1
$CNName.TrimStart('CN=') # this will give you the desired value.

```

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 20, 2021, 5:09pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/7 "2021-04-20T17:09:01Z")

</div>

i got this but here memberof list is more than 50 lines when exported in CSV so its not helping

my code looks like below

$SID =Read-Host -Prompt " Please Provide SID: "  
$Domainlist = Read-Host -Prompt " DOmain ASIAPAC, NAEAST, EMEA "

$r=(Get-Aduser $SID -server $Domainlist -Properties MemberOf | Select MemberOf).MemberOf

$CNName = ($r -split ‘,’)[0] #| Out-File I:$SID.csv  
$CNName.Trimstart(‘CN=’)

this is only checking and providing result of first group but user is part of multiple group, i guess spilt is removing rest all enteries

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 20, 2021, 5:18pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/8 "2021-04-20T17:18:09Z")

</div>

I think you want all the groups where a user is part of. If so

```auto
Get-ADPrincipalGroupMembership username | Select-Object -ExpandProperty name

```

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 20, 2021, 5:25pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/9 "2021-04-20T17:25:57Z")

</div>

> [@kvprasoon](#):
>
> `Get-ADPrincipalGroupMembership username | Select-Object -ExpandProperty name`

before we tried get-aduser we tried ADPrincipalGroupMembership cmdlet but its not working in our environment  
'it says Get-ADPrincipalGroupMembership : An unspecified error has occurred"  
so we are trying other option like get-aduser

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 20, 2021, 8:54pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/10 "2021-04-20T20:54:42Z")

</div>

> [@nik3387](#):
>
> $SID =Read-Host -Prompt " Please Provide SID: "  
> $Domainlist = Read-Host -Prompt " DOmain ASIAPAC, NAEAST, EMEA "
> 
> $r=(Get-Aduser $SID -server $Domainlist -Properties MemberOf | Select MemberOf).MemberOf
> 
> $CNName = ($r -split ‘,’)[0] #| Out-File I:$SID.csv  
> $CNName.Trimstart(‘CN=’)

$r should be a collection of objects, so you can process it with a foreach loop. I’m assuming from your code that you want a CSV file per user.

Not tested:

```auto
$SID =Read-Host -Prompt " Please Provide SID: "
$Domainlist = Read-Host -Prompt " DOmain ASIAPAC, NAEAST, EMEA "

$r = Get-Aduser $SID -server $Domainlist -Properties MemberOf | Select-Object -ExpandProperty MemberOf

foreach ($group in $r) {

    $CName = ($group -split ‘,’)[0]
    $CNName.Trimstart(‘CN=’) | Out-File -PSPath "I:\$SID.csv" -Append -NoClobber

}

```

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 21, 2021, 10:31am UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/11 "2021-04-21T10:31:26Z")

</div>

this is giving only one memberof to and make it correct and removing all others memberof in CSV it display same group name next row repeating 70+ time like

UNAEAST\_DMM\_DESKTOP  
UNAEAST\_DMM\_DESKTOP  
UNAEAST\_DMM\_DESKTOP and so on

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 21, 2021, 11:47am UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/12 "2021-04-21T11:47:35Z")

</div>

You should b easily able to debug the above code. Hope you are using VScode or atleast PowerShell ISE.  
you can create a breakpoint then start debugging, it will help you to catch the bug.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 21, 2021, 12:02pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/13 "2021-04-21T12:02:31Z")

</div>

> [@matt-bloomfield](#):
>
> `$CNName.Trimstart(‘CN=’)`

**Attention!!!**  
Just to at least mention it once. `.TrimStart()` does not work as you might think it does. If the CN starts with a “C” or an “N” or both it would cut off more than you actually wanted to. See this examples:

```auto
'CN=Canada'.TrimStart('CN=')
'CN=NorthDacota'.TrimStart('CN=')
'CN=NC(NorthCarolina)'.TrimStart('CN=')

```

😉  
Take a very close look to the output!!

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 21, 2021, 12:06pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/14 "2021-04-21T12:06:28Z")

</div>

issue here is its accepting first memberof group and it provides accurate result but in this loop its copy pasting total number of group with same first group… not sure if i am able to explain it… i am suing Powershell ISE here

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 21, 2021, 12:31pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/15 "2021-04-21T12:31:41Z")

</div>

There’s a typo in my post, `$CName` instead of `$CNName`. Did you spot that and correct it?

Taking into account @Olaf’s comment. I have amended the code as below to use the replace operator with a regex.

```auto
$SID =Read-Host -Prompt " Please Provide SID: "
$Domainlist = Read-Host -Prompt " DOmain ASIAPAC, NAEAST, EMEA "

$r = Get-Aduser $SID -server $Domainlist -Properties MemberOf | Select-Object -ExpandProperty MemberOf

foreach ($group in $r) {

    $group -replace 'CN=(.+?),.+','$1' | Out-File -PSPath "I:\$SID.csv" -Append -NoClobber

}

```

---

<div class="post-metadata">

**Author:** ![nik3387](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nik3387](https://forums.powershell.org/u/nik3387)\
**Post date:** [April 21, 2021, 12:42pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/16 "2021-04-21T12:42:13Z")

</div>

@matt-bloomfield Wow this is working as expected, i was so struggling since more than 5 days but it has fixed, can you explain this line logic $group -replace ‘CN=(.+?),.+’,‘$1’

$group is user variable or we created, what is this $1 denotes

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 21, 2021, 12:47pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/17 "2021-04-21T12:47:10Z")

</div>

> [@nik3387](#):
>
> $group -replace ‘CN=(.+?),.+’,’$1’

Actually this would be enough as well:

```auto
$group -replace '^CN='

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 21, 2021, 12:59pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/18 "2021-04-21T12:59:34Z")

</div>

The brackets in the match part of the `-replace` operator declare a capture group. So this `(.+?)` matches the text after `CN=` up to the first comma. `$1` is an automatic variable, that has the value of that capture group.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 21, 2021, 1:02pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/19 "2021-04-21T13:02:08Z")

</div>

> [@Olaf](#):
>
> `$group -replace '^CN='`

That only, strips the `CN=` though. OP wanted just the group name without the OU and domain information.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 21, 2021, 1:24pm UTC](https://forums.powershell.org/t/multiple-domain-search-with-user/16295/20 "2021-04-21T13:24:03Z")

</div>

> [@matt-bloomfield](#):
>
> That only, strips the `CN=` though. OP wanted just the group name without the OU and domain information.

I had in my mind that the rest of the CN is already stripped away with the `-split` operator before. So - assumed that there will not be a comma inside the common name - this should do the trick:

```auto
($group -split ',')[0] -replace '^CN='

```

😉  
… another version would be this:

```auto
($group -split '=')[1] -replace ',(OU)|(DC)$'

```

[Next page](https://forums.powershell.org/t/multiple-domain-search-with-user/16295.md?page=2)
