powershelladgroup

PowerShell AD Group Creation from full DN


I'm working on a PS-Script to automatically create AD-Groups from our Corporate Directory.

The group-parameter is looking like this:

“CN=TEST_FOLDER_RW,OU=Groups,OU=Managed,OU=europe,DC=company,DC=com”

But the required syntax for New-ADGroup would be

-Path "ou=mfg,dc=noam,dc=corp,dc=contoso,dc=com"

and

-name "TEST_FOLDER_RW"

instead of the CN= at the beginning.

Could you give me a hint or advise me on how to do that properly?

The only way which came to my mind would be parsing the String between "CN=*," But I've never done this before and I'm not sure if this is even a good way.

Thank you in advance.

Michael


Solution

  • One solution:

    $dn = "CN=TEST_FOLDER_RW,OU=Groups,OU=Managed,OU=europe,DC=company,DC=com"
    $path  = $dn.Split(",")
    
    $name = $path[0] -replace 'CN=',''
    $path  = ($path[1..$path.Count]) -join ','
    

    Then $name and $path equal to:

    $name
    TEST_FOLDER_RW
    $path
    OU=Groups,OU=Managed,OU=europe,DC=company,DC=com