Search code examples
powershellexport-to-csv

Issues exporting powershell to CSV


It has been many moons since I have last done this and I am having problems exporting some commands to CSV. The biggest one that is getting me right now is

$ADGroupList = Get-ADGroup -Filter * -property * | Select Name -ExpandProperty Name | Sort
ForEach($Group in $ADGroupList) 
{
Write-Host "Group: "$Group.Name
Get-ADGroupMember -Identity $Group | Select Name -ExpandProperty Name | Sort
Write-Host ""
}
Export-Csv -path "c:\Temp\test675.csv"

Works fine with out trying to export but the second I try to export the command will run and either generate a blank file or no file at all.

I am able to run other commands with out a issue exporting them to csv. Thanks for any help in advance.


Solution

  • Tim,

    From what I can see you're not giving Export-Csv anything to write. Try this:

    $ADGroupList = Get-ADGroup -Filter * -property * | Select Name -ExpandProperty Name | Sort
    ForEach($Group in $ADGroupList) 
    {
    Write-Host "Group: "$Group.Name
    Get-ADGroupMember -Identity $Group | 
      Select Name -ExpandProperty Name | 
      Sort                             |
      Export-Csv -path "c:\Temp\test675.csv"
    Write-Host ""
    }
    

    I'd test this but I don't have access to ActiveDirectory. Also the Write-Hosts you probably don't want in the .csv file as they won't play well with the format, headers & columns.

    HTH