Search code examples
powershellou

Extract OU and Domain from DistinguishedName


I have a user named Lamda that I want to extract the OU and Domain sequence from the DistinguishedName.

For example, for the DistinguishedName of CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD

I want to extract the sequence OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD and assign to a variable.

I tried the following script but it did not work:

$var = dsquery user -name 'Lamda' | dsget user -dn [regex]::match($var,'(?=OU)(.*\n?)(?<=.)').Value

Solution

  • Split by non-escaped comma once, discard the first substring:

    $null,$ou = 'CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD' -split '(?<!\\),',2