powershell -SearchBase doesn’t accept OU with spaces

Welcome Forums General PowerShell Q&A powershell -SearchBase doesn’t accept OU with spaces

Viewing 7 reply threads
  • Author
    Posts
    • #248755
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      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 ??

      • This topic was modified 1 month, 1 week ago by grokkit.
    • #248761
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      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

       

      • This reply was modified 1 month, 1 week ago by grokkit.
    • #248764
      Participant
      Topics: 0
      Replies: 11
      Points: 118
      Helping Hand
      Rank: Participant

      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.

      • This reply was modified 1 month, 1 week ago by Bill Riedy.
    • #248773
      Participant
      Topics: 25
      Replies: 184
      Points: 658
      Helping Hand
      Rank: Major Contributor

      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.

    • #248794
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

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

    • #248809
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      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: (:) [Get-ADUser], ADIdentityNotFoundException
      + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
      icrosoft.ActiveDirectory.Management.Commands.GetADUser

       

    • #248830
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      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 ??

    • #248935
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      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

Viewing 7 reply threads
  • You must be logged in to reply to this topic.