csv output with variable from a function

function Get-NestedMembers
        # Param1 help description



        $ADMembers=Get-ADGroupMember -Identity $ADGroup

        $ADMembers | ForEach-Object {
                     if ($_.objectClass -eq 'Group')
                        Get-NestedMembers -ADGroup $_.Name
                     }#end if
                     }#end else    
                     }#end foreach 
    }#end Process
}#end function Get-NestedMembers

Get-NestedMembers | Export-Csv 'ADGroup2.csv' -NoTypeInformation

I have this function to get nested group members and the output is currently fine. However when the function runs it loses the variable $ADGroup which I believe is correct in PowerShell. Is there way to pass the variable so I can export-csv ‘$ADGroup.csv’. With my current file I have to rename it to the group name after each time I run this.

So, your Begin/Process/End blocks are not used here, because your command doesn’t accept pipeline input.

I don’t know what you mean by “lose” $ADGroup. You’ve defined $ADGroup as an input parameter. I would expect:

Get-NestedMembers -ADGroup "something"

When you run the command. That’s how $ADGroup gets populated. But it only exists within Get-NestedMembers; there’s no way for that parameter to be passed down the pipeline to another command like Export-Csv. What you’d do instead is write a small “controller” script.

Param( [string]$ADGroup ) Get-NestedMembers -ADGroup $ADGroup | Export-CSV "$ADGroup.csv"

You’d then run that script, passing in whatever -ADGroup parameter you’d want.

I started with the snippets so I just left the Begin/Process/End blocks.
When I run it with:
Get-NestedMembers -ADGroup $ADGroup | Export-CSV “$ADGroup.csv”

This gets me this error.

Get-NestedMembers : Cannot bind argument to parameter ‘ADGroup’ because it is an empty string.
At C:\PSScripts\Functions\Get-NestedMembers1.ps1:55 char:28

  • Get-NestedMembers -ADGroup $ADGroup | Export-CSV “$ADGroup.csv”
  •                        ~~~~~~~~
    • CategoryInfo : InvalidData: (:slight_smile: [Get-NestedMembers], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Get-NestedMembers

I must be missing something. I believe by design it removes the variable $ADGroup after the function runs so it is an empty string. Generally I run this and let it prompt for the ADGroup. That make it easiest to repeat for multiple groups.

Yeah, you have to define $ADGroup before you can use it.

$Thing = "something"
Get-NestedMembers -ADGroup $Thing | Export-CSV "$Thing"

You’re likely getting confused because the same ADGroup variable name was being used in different scopes. Here, I’ve used a different variable name to try and make it clearer.

That does work but it requires me to reset the $ADGroup for each group before running the function. Here is one I have as a script that does about the same thing but I haven’t converted it to a function yet for the same reason. Sorry if this is a bit weird.
Since it is not a function it passes through and names the folder by the group name.

    Param (

Get-ADGroupMember -Identity $ADGroup |
    Select-Object -Property Name,
                        DistinguishedName |
 Export-Csv "$ADGroup.csv" -NoTypeInformation