UTF-8 encoding in powershell script RRS feed

  • Вопрос

  • Hello,

    I am using the following PowerShell script (many lines are removed to make it short)  and I m facing issues in querying users with their distinguishedname as some of the users has non-English characters in the DN. How can I use UTF-8 encoding inside my script to query my users correctly? Any help would be highly appreciated.

    #region User
    $Properties = @("objectGuid","sAMAccountName","isDeleted")
    $Credentials = New-Object System.Net.NetworkCredential($username,$password)
    $RootDSE = [ADSI]"LDAP://RootDSE"
    $LDAPDirectory = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($RootDSE.dnsHostName)
    $LDAPConnection = New-Object System.DirectoryServices.Protocols.LDAPConnection($LDAPDirectory, $Credentials)
    $Request = New-Object System.DirectoryServices.Protocols.SearchRequest($RootDSE.defaultNamingContext, "(&(objectClass=user)(sAMAccountName=T*))", "Subtree", $Properties)
    [System.DirectoryServices.Protocols.SearchResultEntry]$entry = $null
        $Response = $LDAPConnection.SendRequest($Request)
        ForEach($entry in $Response.Entries){
     $DirEntry = New-Object System.DirectoryServices.DirectoryEntry "LDAP://$($entry.distinguishedName)"


    18 июля 2019 г. 1:54


  • Nothing needs to be changed. You do not need to use those commands with AD. Just use ADSI and ADSISearcher.

    $searcher = [adsisearcher]'(objectClass=user)'

    You can set the properties on the search to do all of the other things.  The DN will take care of itself.


    • Изменено jrv 18 июля 2019 г. 2:30
    • Помечено в качестве ответа Abu Ameen 25 июля 2019 г. 14:59
    18 июля 2019 г. 2:29

Все ответы