1

I'm working on creating a Powershell script that pulls nested distribution groups. My idea is that it will check the parent group for any children groups, then pull that group, rinse & repeat. I'm very new to PS to I might be getting hung up on something simple (hopefully).

Right now, I know how to pull what I want. I'm using:

$finallist = Get-DistributionGroupMember -Identity "DISTRO GROUP HERE" | Select Name, PrimarySMTPAddress

This works fine. I'm also able to do the following and have it add to the variable:

$finallist += Get-DistributionGroupMember -Identity "NEXT DISTRO" | Select Name, PrimarySMTPAddress

However, the problem that I'm having is that I cannot use += beginning with an empty initialized variable. At the start of the script, I of course initialize it by using $finallist = "". If I were to use the += version of the command immediately after, it results in a completely empty string. Making it equal to the first distribution group, then systematically adding each group after using a += works totally fine.

Hopefully I've described my issue adequately. If not, I'm happy to reiterate what I mean. Thanks a ton guys.

Fizzik
  • 21
  • 3
  • 3
    I don't think I'm understanding the issue, but if you're looking to save to a variable there's a plenty of ways you can go about it. `+=` is the least preferred way to go about it. The other options are, making a use of lists, or letting PowerShells streaming save to a variable; with the latter being preferred. Example: `$results = foreach ($group in $groups) { ... }`, where the results all get added to `$results`. – Abraham Zinala Sep 12 '22 at 20:09
  • Seems like you're looking to query `Distribution Groups` recursively, if that's the case you will need either a function that calls itself or a `Stack` – Santiago Squarzon Sep 12 '22 at 20:12
  • @AbrahamZinala long story short, += works if the variable is not empty, but does not work if it is. – Fizzik Sep 12 '22 at 20:17
  • 1
    You could declare it as an array, like `[array]$finallist +=` and it should work fine. – TheMadTechnician Sep 12 '22 at 20:21
  • @TheMadTechnician I will definitely give that a shot man, thank you. – Fizzik Sep 12 '22 at 20:23
  • 1
    As an aside, starting with ```$finallist = ""``` will result in ```$finallist += ...``` doing *string* concatenation, not *array* concatenation. Compare ```$x = ""; $x += @( "a", "b", "c" ); $x | convertto-json``` which gives ```"a b c"``` vs ```$x = @(); $x += @( "a", "b", "c" ); $x | converrto-json``` which gives ```[ "a", "b", "c" ]```. See "The Assignment By Addition Operator" documentation for details: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators?view=powershell-7.2#the-assignment-by-addition-operator – mclayton Sep 12 '22 at 20:37
  • 1
    as @TheMadTechnician said to go for arrays and @mclayton showed to initialise the variable to `$finallist` as an array here is my comment: first initialize the variable by using `$finallist = @()` this defines it as an array. thereafter always use `+=` to assign results to it. – Guenther Schmitz Sep 12 '22 at 21:02
  • 2
    @AbrahamZinala provided the most efficient approach to capture the nested groups one level down instead of using `@()` and `+=`. – Santiago Squarzon Sep 12 '22 at 21:09
  • The linked duplicate's problem is _neglecting to initialize_ the array output variable, whereas you're mistakenly initializing it _as a string_ (`$finallist = ""`). However, the solution is the same - as is the far superior alternative of letting _PowerShell_ collect the outputs from an entire loop or pipeline in an array. – mklement0 Sep 12 '22 at 21:30

0 Answers0