Read from File - Query AD


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)




Get-Content “c:\temp\ps\input.txt” | Foreach-Object {Get-ADuser -sAMAcountName $PSItem -Property Description,samAccountName,AccountExpirationDate}


[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
    • CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser[/i]

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.”

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 &#8230. 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
    • CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException

It seems four periods translate to this &#8230
Get-ADUser “&#8230

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).

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