powershelldistinguishedname

Keep DC part of distinguished name


I have for input distinguished names like the following:

CN=A00.user,OU=MyOU,OU=A00,OU=MyOU3,DC=my,DC=domain
CN=A01.user1,OU=MyOU1,OU-MyOU2,OU=A00,OU=MyOU3,DC=my,DC=first,DC=domain

I need to print only the DC part, to get an output like:

my.domain
my.first.domain

Looks like split or replace should work, but I'm having trouble figuring out the syntax.


Solution

  • I would simply remove everything up to and including the first ,DC= and then replace the remaining ,DC= with dots.

    $dn = 'CN=A00.user,OU=MyOU,OU=A00,OU=MyOU3,DC=my,DC=domain',
          'CN=A01.user1,OU=MyOU1,OU-MyOU2,OU=A00,OU=MyOU3,DC=my,DC=first,DC=domain'
    
    $dn -replace '^.*?,dc=' -replace ',dc=', '.'