Home > Net >  Powershell - Export Count of Groups Members to excel
Powershell - Export Count of Groups Members to excel

Time:01-04

       foreach ($group in $ADMSObjects){
    
            $searchString = $group.GroupDisplayName
            $count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT
            write-host $group.GroupDisplayName  , $count 
            
       }

I just need the function 'extended' to actually export $group.GroupDisplayName and $count to an Excel Sheet.

When I try to do something like:

       foreach ($group in $ADMSObjects){
    
            $searchString = $group.GroupDisplayName
            $count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT
            write-host $group.GroupDisplayName  , $count 
            Export-Csv -Path $path $group, $count -Append
            
       }
DLAS - AMER - Global Planning and Transformation - 1190-GO-CGPT10026 32
Export-Csv : Cannot convert 'System.Object[]' to the type 'System.Char' required by parameter 
'Delimiter'. Specified method is not supported.
At C:\users\crxdan\Documents\Set-DLASGroups.ps1:220 char:36
              Export-Csv -Path $path $group, $count -Append
                                     ~~~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
      FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

I'm not really sure where to go from here.

CodePudding user response:

you could use PSCustomObject to get your results in an order. I guess the $group you mentioned is group display name. I do not have an environment to test it. so please test it from your end. Thanks!

$result = foreach ($group in $ADMSObjects){
    
   $searchString = $group.GroupDisplayName
   $count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT
   write-host $group.GroupDisplayName  , $count

    [PSCustomObject]@{
            Group = $group.GroupDisplayName 
            Count = $Count
        }
}
$result | Export-Csv -Path $path -Append

CodePudding user response:

I tried to reproduce the scenario in my environment:

Note: To print display name: Run using $group.DisplayName

Used below code:

$azgroups=Get-AzureADGroup
foreach ($group in $azgroups){
$searchString = $group.GroupDisplayName
$count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT

write-host $group.DisplayName  , $count

$group_properties = [pscustomobject] @{
"DisplayName" = $group.DisplayName
"Count" = $count
}
$group_properties | Export-csv -Path  C:\User\xxx\myexcelsheet" -NoTypeInformation -Force -Append
}

enter image description here

The csv or excel file is created successfully:

enter image description here

And we could see the required fiels in the csv file

enter image description here

  • Related