Get-aduser with filter from txt file

HMIMED The king 21 Reputation points
2022-11-03T10:16:58.79+00:00

Hello
please help me to fix this case:
i have text file content all displayname with header displayname, i want to excute this one:
import-module ActiveDirectory
$users= Get-Content c:\scripts\displaynames.txt

foreach ($user in $users){

get-aduser -filter {DisplayName eq $_.displayname} | Select-Object -Property samaccountname,displayname

}

i get error :
get-aduser : Erreur lors de l’analyse de la requête : « DisplayName eq $*.displayname » Message d’erreur : « syntax error » à la position : « 13 ».
Au caractère Ligne:5 : 1

  • get-aduser -filter {DisplayName eq $*.displayname} | Select-Object - ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

get-aduser : Erreur lors de l’analyse de la requête : « DisplayName eq $*.displayname » Message d’erreur : « syntax error » à la position : « 13 ».
Au caractère Ligne:5 : 1

  • get-aduser -filter {DisplayName eq $*.displayname} | Select-Object - ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,361 Reputation points MVP
    2022-11-03T18:19:21.953+00:00

    Hi @HMIMED The king ,

    this script is working with the text file without any problems here:

    Textfile Content:

    Jon Doe  
    Abraham Linkon  
    Peter Pan  
    John Wayne  
    

    Script:

    Import-Module ActiveDirectory  
    Get-Content C:\Scripts\displaynames.txt |  
    ForEach-Object {  
        $user = $_.Trim()  
        Get-ADUser -Filter "DisplayName -eq '$user'" -Properties DisplayName |   
        Select-Object -Property SamAccountName, DisplayName  
    }  
    

    Result:

    256942-image.png

    ----------

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

    Regards
    Andreas Baumgarten

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,361 Reputation points MVP
    2022-11-03T13:52:40.943+00:00

    Hi @HMIMED The king ,

    instead of equals ( -eq ) you cann try -like as well:

    Get-ADUser -Filter "DisplayName -like '$user'" -Properties DisplayName | Select-Object -Property SamAccountName, DisplayName      
    

    ----------

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

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 44,776 Reputation points
    2022-11-03T15:10:25.43+00:00

    Are you sure that the file containing the display names has no leading or trailing space characters? Can you attach a file containing, say, the first few lines of that file?

    In the meantime, try trimming the data befor you use it:

    Get-Content c:\scripts\displaynames.txt |  
        ForEach-Object {  
            $user = $_.Trim()  
            Get-ADUser -Filter "DisplayName -eq '$user'" |   
                Select-Object -Property samaccountname, displayname  
        }  
    

    Keep in mind that the contents of the DisplayName property is not guaranteed to be unique in the AD.


  3. HMIMED The king 21 Reputation points
    2022-11-03T15:47:46.177+00:00
    0 comments No comments