title: Powershell: How to obtain the complete list of distribution groups
body: <p>I am completely new to powershell, I have never touched this scripting language before. However, I have some backgrounds in perl and bash scripting. I am trying to implement a small script that will obtain the list of DG in Exchange server, filters the results to get only the groups that have a certain string, corresponding to the current year.</p>
  
  <p>Example: check the year, in this case 2011.
  Filter Name Contains 'P11' 
  Return only the last DG name and parse the first 7 characters.</p>
  
  <p>How could I do this using powershell from an exchange server? Here is what I got:</p>
  
  <pre><code>add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
  
  # Retrieve all DGs
  $temp = Get-DistributionGroup -ResultSize Unlimited | 
  
  foreach($group in $temp) 
  { 
  write-output "GroupName:$group " 
  Write-output "GroupMembers:" 
  Get-DistributionGroupMember $group |ft displayname,alias,primarysmtpaddress 
  write-output ‘ ‘ 
  
  }
  </code></pre>
  
  <p>this results in the following error:</p>
  
  <blockquote>
    <p>Unexpected token 'in' in expression or statement. At
    C:\Users\jfb\Desktop\NewGroupProject.ps1:7 char:18
    + foreach($group in &lt;&lt;&lt;&lt;  $temp)
        + CategoryInfo          : ParserError: (in:String) [],
    ParseException
        + FullyQualifiedErrorId : UnexpectedToken</p>
  </blockquote>