# Read from File - Query AD

**URL:** <https://forums.powershell.org/t/read-from-file-query-ad/3625>\
**Category:** PowerShell Help\
**Created:** [January 14, 2015, 3:33am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625 "2015-01-14T03:33:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-kirkpatrick](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@mike-kirkpatrick](https://forums.powershell.org/u/mike-kirkpatrick)\
**Post date:** [January 14, 2015, 3:33am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/1 "2015-01-14T03:33:02Z")

</div>

Hello,

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)

ie:

smithj  
johnsonb  
millerr

collj

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

ERROR

[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]

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [January 14, 2015, 3:45am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/2 "2015-01-14T03:45:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike-kirkpatrick](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@mike-kirkpatrick](https://forums.powershell.org/u/mike-kirkpatrick)\
**Post date:** [January 14, 2015, 3:54am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/3 "2015-01-14T03:54:57Z")

</div>

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  
ate}
  - CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException  
[/i]

---

<div class="post-metadata">

**Author:** ![mike-kirkpatrick](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@mike-kirkpatrick](https://forums.powershell.org/u/mike-kirkpatrick)\
**Post date:** [January 14, 2015, 3:55am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/4 "2015-01-14T03:55:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [January 14, 2015, 3:59am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/5 "2015-01-14T03:59:36Z")

</div>

Yes, that’s just a WordPress thing.

Check out the help for Get-ADUser: [http://technet.microsoft.com/en-us/library/ee617241.aspx](http://technet.microsoft.com/en-us/library/ee617241.aspx)

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

---

<div class="post-metadata">

**Author:** ![mike-kirkpatrick](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@mike-kirkpatrick](https://forums.powershell.org/u/mike-kirkpatrick)\
**Post date:** [February 19, 2015, 4:17am UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/6 "2015-02-19T04:17:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/read-from-file-query-ad/3625/7 "2024-05-16T20:46:19Z")

</div>


