get base OU name for a list of users

Eaven HUANG 2,191 Reputation points
2022-08-11T03:20:03.023+00:00

Dear experts,

I'm having an issue when trying to get the basename of the OUs for a list of users. I have a .csv file and trying following scripts but @OUname didn't give me the result.

How get I use the OU name without that CN=XXX part?
Thanks a lot!

foreach ($User in $ADUsers)  
{  
 #Read user data from each field in each row and assign the data to a variable as below  
  
    $department = $User.Department  
    $Username  = $User.Account  
    $email      = $User.Email  
    $employeeid = $User.EmployeeID  
    $jobtitle   = $User.JobTitle  
    $employeeType = $User.employeeType  
    $group1     = $User.Group1  
    $group2     = $User.Group2  
  
  
    $DN = Get-ADUser -Identity $Username -Properties * | select DistinguishedName  
    $OUname = Get-ADOrganizationalUnit -Identity $DN -Properties * | select name  

I've defined $DN but the next $OUname went wrong with errors:
PS C:\Users\eaven.huang> $DN

DistinguishedName                                                                    
-----------------                                                                    
CN=Eaven.test,OU=IT Test,OU=Admins,OU=Staff,OU=Users,OU=XXX,DC=cn  
  
  
  
PS C:\Users\eaven.huang> $OUname = Get-ADOrganizationalUnit -Identity $DN  
Get-ADOrganizationalUnit : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.  
At line:1 char:46  
+ $OUname = Get-ADOrganizationalUnit -Identity $DN  
+                                              ~~~  
    + CategoryInfo          : InvalidData: (:) [Get-ADOrganizationalUnit], ParameterBindingValidationException  
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADOrganizationalUnit  
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Dillon Silzer 57,831 Reputation points Volunteer Moderator
    2022-08-11T04:12:49.587+00:00

    Hi @Eaven HUANG

    Just from a quick glance it looks like your DN structure may be incorrect (CN=Eaven.test,OU=IT Test,OU=Admins,OU=Staff,OU=Users,OU=XXX,DC=cn). Usually it should end with the domain being DC=XXX,DC=cn

    Example for domain FABRIKAM.COM:

    OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-adorganizationalunit?view=windowsserver2022-ps#example-3-get-child-ous

    If you still can't get it to work, can you paste more of your PowerShell script?

    ---------------------------------

    If this is helpful please accept answer.

    0 comments No comments

  2. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2022-08-11T06:19:06.477+00:00

    Hi @Eaven HUANG ,

    the issue is: The DistinguishedName (DN) property of the user contains at the beginning the user details (CN=xxxx,) followed by the OU/domain structure.
    You need to split the user's DN to get the OU details only.

    Please try this to get just the OU of the user:

    $Username = "mmouse"  
    $DN = ((Get-ADUser -Identity $Username).DistinguishedName).Split(",",2)[1]  
    $OUname = Get-ADOrganizationalUnit -Identity $DN -Properties * | select name  
    $OUname  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Limitless Technology 39,926 Reputation points
    2022-08-11T15:29:07.67+00:00

    Hello there,

    Maybe this will help.

    $OUpath = 'ou=Managers,dc=enterprise,dc=com'
    $ExportPath = 'c:\data\users_in_ou1.csv'
    Get-ADUser -Filter * -SearchBase $OUpath | Select-object
    DistinguishedName,Name,UserPrincipalName | Export-Csv -NoType $ExportPath

    Use a Single Line PowerShell Command to List All Users in an OU https://devblogs.microsoft.com/scripting/powertip-use-a-single-line-powershell-command-to-list-all-users-in-an-ou/

    ----------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  4. Rich Matheisen 47,901 Reputation points
    2022-08-11T18:19:12.713+00:00

    You can get the 1st OU name from a distinguished name like this:

    $x = "CN=Eaven\, test,OU=IT Test,OU=Admins,OU=Staff,OU=xxx"  
      
    $x -match "^((CN=.*?))?OU=(?<OUName>.*?(?=,))"  
    $matches.OUName
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.