I had logic in my script to display specific messages when the array count was zero or 1.
1 was never working so I investigated further.
Whenever my array had a size of 1, the ‘.Count’ value was always Null.
I found this to be the case with arrays created by commands: Get-DistributionGroupMember, Get-AzureADGroupOwner, Get-AzureADGroupMember, and Get-MailboxPermission
$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'https://outlook.office365.com/PowerShell-LiveId/' -Credential XXXXXXXXXXXXXXX -Authentication Basic -AllowRedirection
Import-PSSession $PSSession -DisableNameChecking -AllowClobber
$DistributionList = Get-DistributionGroup | Select DisplayName, Identity | Sort DisplayName
ForEach ($D in $DistributionList) {
Write-Output $D.DisplayName
$Members = Get-DistributionGroupMember -Identity $D.Identity | Select DisplayName, Id | Sort DisplayName
Write-Output $Members.Count
[Int]$Count = 0
ForEach ($M in $Members) {
$Count++
}
Write-Output "ForEach count: $Count"
}
Remove-PSSession $PSSession
Results showing 0, 1, and 27 Members (notice there is no ‘.Count’ displayed for ‘A’:
A
ForEach count: 1
B
0
ForEach count: 0
C
27
ForEach count: 27
Oddly enough, the count works for 1 when the array is simple:
$myArray = @()
Write-Output $myArray.Count
$myArray = @('A')
Write-Output $myArray.Count
$myArray = @('A', 'B', 'C')
Write-Output $myArray.Count
Results:
0
1
3
My PS Version Info
$psversiontable
Name Value
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1