title: How to list AD group membership for AD users using input list?
body: <p>I'm fairly new PS user... Looking for some assistance with a powershell script to obtain list of security groups user is member of.</p>
  
  <p>To describe what I need:</p>
  
  <ul>
  <li>I have input list (txt file) with many users (samaccountnames). Every name is on a new line.</li>
  <li>I need the script to search these names in AD - whole forest, not just one single domain</li>
  <li>output should look like "samaccountname" and list of groups this account is member of in one line, so I can sort it in excel</li>
  </ul>
  
  <p>This is the script I have:</p>
  
  <pre><code>$users = Get-Content C:\users.txt
  
  ForEach ($User in $users) {
    $getmembership = Get-ADUser $User.Users -Properties MemberOf | Select -ExpandProperty memberof
    $getmembership | Out-File -Append c:\membership.txt 
  }
  </code></pre>
  
  <p>but it throws me an error:</p>
  
  <pre><code>Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
  At line:4 char:28
  + $getmembership = Get-ADUser &lt;&lt;&lt;&lt;  $User.Users -Properties MemberOf | Select -ExpandProperty memberof
      + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
  </code></pre>
  
  <p>Anyway, this script wouldn't search the whole forest.</p>
  
  <p>Sample input list:</p>
  
  <pre><code>username1
  username2
  username3
  username4... etc
  </code></pre>
  
  <p>Sample output list</p>
  
  <pre><code>username1;group1;group2;group3
  username2;group1;group2;group3;group4... etc or something similar
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>