title: Get AD Group Names from List and Group Members Properties with Powershell
body: <p>I'm trying to <code>Get-Content</code> from a text file which includes AD group names.  Then from this group list get the AD user properties for each member <code>samaccountname</code>, <code>name</code>, <code>enabled</code> properties of the user.  The output I'd like to see would look something like this for each group in the text file.</p>
  
  <pre><code>Group Name
  samaccountname  name  enabled
  </code></pre>
  
  <p>I managed to get the correct output but cannot get it output properly as the <code>Write-Host</code> result cannot be output to a file.  Here is the script that gets what I want (well sort of - the first group name appears above the header).</p>
  
  <pre><code>$ErrorActionPreference = "SilentlyContinue"  
  Get-Content D:\ADGroups.txt | Where{$_ -ne $null -and $_.trim() -ne ""} |   
      foreach{  
          $Group = $_   
          Write-Host "$Group"  
          Get-ADGroup -Identity $Group -Properties members |  
              Select-Object -ExpandProperty members |  
              Get-ADUser -Properties samaccountname, enabled |  
              Select samaccountname, name, Enabled  
      }   
  </code></pre>
  
  <p>I've searched and found similar scripts but none of them produced the results I'm looking for.</p>