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,359 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,036 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,036 Reputation points MVP
    2022-11-03T10:39:39.317+00:00

    Hi @HMIMED The king ,

    if you use Get-Content c:\scripts\displaynames.txt the txt file contains just the DisplayName of each user in each row and now additional columns?

    If "yes" please try this:

    Import-Module ActiveDirectory  
    $users = Get-Content c:\scripts\displaynames.txt  
    foreach ($user in $users) {  
        Get-ADUser -Filter "DisplayName -eq '$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

    1 person found this answer helpful.
    0 comments No comments

  2. chirag darji 131 Reputation points
    2022-11-03T10:35:26.13+00:00

    Hi @HMIMED The king

    Cab you share please your .txt file. how to fill data into .TXT file

    0 comments No comments

  3. HMIMED The king 21 Reputation points
    2022-11-03T12:34:21.877+00:00

    hi thanks for reply
    bu when Import-Module ActiveDirectory
    $users = Get-Content c:\scripts\displaynames.txt
    foreach ($user in $users) {
    Get-ADUser -Filter "DisplayName -eq '$user'" -Properties DisplayName | Select-Object -Property SamAccountName, DisplayName
    }

    it send the same line of powershell et no result of AD

    i think the problem is on eq $user same with -like how i can fix it.

    thanks

    0 comments No comments

  4. HMIMED The king 21 Reputation points
    2022-11-03T12:34:22.56+00:00

    hi thanks for reply
    bu when Import-Module ActiveDirectory
    $users = Get-Content c:\scripts\displaynames.txt
    foreach ($user in $users) {
    Get-ADUser -Filter "DisplayName -eq '$user'" -Properties DisplayName | Select-Object -Property SamAccountName, DisplayName
    }

    it send the same line of powershell et no result of AD

    i think the problem is on eq $user same with -like how i can fix it.

    thanks

    0 comments No comments