Read from File - Query AD

This topic contains 5 replies, has 2 voices, and was last updated by  Mike Kirkpatrick 3 years, 1 month ago.

  • Author
  • #21856

    Mike Kirkpatrick


    I'm struggling with reading content from a file and then using that content to act as the input to a query of Active Directory.
    Any help would be greatly appreciated, I am more than wet behind the ears.

    My input file is formatted as single entry and CRLF. yes there are spaces\blanks in the file (as seen below)




    [i]Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -sAMAcountName $PSItem -Property Description,samAccountName,AccountExpirationDate}[/i]


    [i]Get-ADUser : A parameter cannot be found that matches parameter name 'sAMAcountName'.
    At line:1 char:79
    + Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -sAMAcountName <<<< $PSItem -Property Description,samAccountName,AccountExpirationDa te} + CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser[/i]

  • #21857

    Don Jones

    The blanks are going to cause a problem for you. If those are unavoidable, you're probably going to have to go with a more script-like approach that can test for that.

    foreach ($line in (Get-Content input.txt)) {
      if ($line.trim() -ne '') {
        Get-ADUser ...

    Your other problem (and the reason for the error) is that Get-ADUser doesn't have a -samAcountName parameter. It's -samAccountName – you missed a "c."

  • #21858

    Mike Kirkpatrick

    Thank you for the quick reply Don.

    I changed the property to sAMAccountName (typos, what a rookie) and I still error out as initially shown.
    Also, you've written Get-ADUser …. I'm not sure what that means, perhaps an escape character issue with posting to wordpress??

    [i]Get-ADUser : A parameter cannot be found that matches parameter name 'samAccountname'.
    At line:2 char:80
    + Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -samAccountname <<<< $PSItem -Property Description,samAccountName,AccountExpirationD ate} + CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException [/i]

  • #21859

    Mike Kirkpatrick

    It seems four periods translate to this …
    Get-ADUser "…"

  • #21860

    Don Jones

    Yes, that's just a WordPress thing.

    Check out the help for Get-ADUser:

    Turns out there's no -samAccountName parameter at all :). You're probably wanting to use -Identity, but you should review the help to be sure that's what you're after (since I don't know the contents of your text file).

  • #22751

    Mike Kirkpatrick

    Thank you Don.
    Following your lead I was able to get the script working (albeit painful but that's learning)

You must be logged in to reply to this topic.