Export results of foreach loop to CSV

时间:2016-04-15 14:50:32

标签: csv powershell

My script looks great on console, but I would like to write/export the results to a .csv file. I tried creating a variable, and then pipe the result to another cmdlet, but that doesn't seem to work because of the additional pipe before my loop starts.

Get-ADGroup -Server "company.com" -Properties * -Filter * -SearchBase "OU=Groups,OU=Users,OU=Corporate,..."
| Sort-Object Name 
| ForEach-Object{
    $_ | Select-Object Name,@{
      Name = 'User'
      Expression = {Get-ADGroupMember -Identity $_ 
                   | Select-Object -ExpandProperty name}
    }
  }  
| Where-Object {$_.Name} 

Thanks for your help.

0 个答案:

没有答案