powershell -SearchBase doesn't accept OU with spaces

I need help with a power shell

This command runs very good
Get-ADUser -Filter * -SearchBase “OU=User,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export-csv -path c:\tmp\userexport.csv

But It fail with another OU with spaces in name like this “3 new students/Man/now”
Get-ADUser -Filter * -SearchBase “OU=3 new students,OU=Man,OU=now,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export-csv -path c:\tmp\userexport.csv

Please Advice ??

Hi
i’ve not tested this but what you could try is

$Ou =“3 new Students”

and in your code

get-aduser -Filter * -searchbase “OU=$OU,OU=Man,OU=now,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export"

good Luck
Paul

 

Based upon your post you have the correct syntax. There may be something else afoot.

I would suggest that perhaps what you believe is an OU may not be an OU, but rather another container type
found within Active Directory. I would suggest that you run a get-aduser USERNAME
where USERNAME is a uniquely named account in that particular container in AD.

Look at the DistinguishedName property and that will show you the proper naming convention for
the container you are in.

using the -searchbase, you will need to wrap the distinguishedname of the OU you are trying to search in “”

especially if you’re hard-coding the path.
the variable example is another approach, you can assign the dn as a string wrapped in quotes to a variable, and call that as the value for the searchbase.

This should be your approach for pretty much any property in powershell that contains a space.

If the SearchBase value is correct, you should be able to run the following and receive a result:

# Return the target OU object
Get-ADOrganizationalUnit "OU=3 new students,OU=Man,OU=now,DC=new,DC=go,DC=sa"

# Run this if command above returns nothing
# Return subOU objects of parent OU
# Use this to find the actual DistinguishedName of the target OU
Get-ADOrganizationalUnit -SearchBase "OU=Man,OU=now,DC=new,DC=go,DC=sa" -Filter *

I try this

$Ou =“3 new Students”

Get-ADUser -Filter * -SearchBase “OU=$OU,OU=Man,OU=now,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export-csv -path c:\tmp\userexport.csv

But Fail again with error:

Get-ADUser : Directory object not found
At line:1 char:1

  • Get-ADUser -Filter * -SearchBase "OU=$OU,OU=Man,OU=now,DC=new,D …
  • CategoryInfo : ObjectNotFound: (:slight_smile: [Get-ADUser], ADIdentityNotFoundException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
    icrosoft.ActiveDirectory.Management.Commands.GetADUser

 

Thanks to Mr. Bill Riedy

My first code is work but the OU should be arranged in reverse order I know this After I try

get-aduser USERNAME

which give me info in reverse order so the correct working code is:

Get-ADUser -Filter * -SearchBase “OU=now,OU=Man,OU=3 new students,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export-csv -path c:\tmp\userexport.csv

Now I have a new issue that in result csv file the Description shows as “??? ??? ??? ???”

as we have description is Arabic so this is wrong code … is there is any solution to export description is

Arabic characters as it shows in AD ??

Thanks very much it was encoding and now it works

For Reference my Working code is :-

Get-ADUser -Filter * -SearchBase “OU=now,OU=Man,OU=3 new students,DC=new,DC=go,DC=sa” -Properties * | Select-Object name,mail,Mobile,Description,EmployeeID | export-csv -path c:\tmp\userexport.csv -Encoding “UTF8”

Thanks Again