Modifying a Get-ADGroup cmdlet

I have a cmdlet that returns members of groups based on filter. I have blocked out AD structure of the snippet that is not relevant. The code returns the group name, the user (member),SAMACCOUNT, Group Manager, and Department. I would like to add email. Here is the code. I have tried several things, to no avail. Any help is appreciated.

Get-ADGroup -Filter{name -like "$$$"} -SearchBase "OU, OU, OU, DC, DC" -Properties info, ManagedBy |
    foreach{$grpname = $_.name;$info = $_.info;$mgr = $_.managedby;Get-ADGroupMember -Identity $_.objectguid |
    select @{n="AD_GroupName";e={$grpname}}, @{n="AD_Group_Notes";e={$info}}, `
		@{n="AD_Group_Manager";e={$(Get-ADUser $mgr -Properties cn).cn}}, `
        name, samaccountname,@{Name="Department";Expression =  `
		{Get-ADUser $_.SamAccountName -Properties Department |
         Select -ExpandProperty Department}}}

Gtoml
Welcome to the forum. :wave:t4:

I’d recommend to use a PSCustomObject as it provides a much cleaner look and should be easier to read, to maintain if needed and to understand:

Get-ADGroup -Filter { name -like "$$$" } -SearchBase "OU, OU, OU, DC, DC" -Properties info, ManagedBy |
ForEach-Object { 
    $Group = $_
    Get-ADGroupMember -Identity $_.objectguid |
    ForEach-Object {
        [PSCustomObject]@{
            AD_GroupName     = $Group.name
            AD_Group_Notes   = $Group.Info
            AD_Group_Manager = $(Get-ADUser -Identity $($Group.managedby) -Properties cn).cn
            Name             = $_.name
            sAMAccontName    = $_.samaccountname
            Department       = $(Get-ADUser -Identity $($_.SamAccountName) -Properties Department).Department
        }
    }
}

To make your code look better you should try to avoid using backticks and aliasses and you should try to write your code as explicit as possible. For example: it makes your code easier to read and to understand when you explicitly write the parameter names.

That does look alot better and easier to read (especially for someone green like me). The filter was pulling in many unneeded groups, so I created a text file to limit the groups to. I also added users’ email. Also, I added a ‘where’ statement to limit the Get-GroupMember to just users (was getting errors if a group member was a group (with the Get-AdUser calls on AD_Group_Manager, Department, and Email).

I was also getting errors on those same parameters if it was null. The comonality is the Get-ADUser call (on parameters AD_Group_Manager, Department, and Email). For instance, AD_Group_Notes could be blank and no error.

Anyhow, the output in ISE looks good. I am wanting to export this to a CSV file, so I thought I would add the pipe at the end, however, I am getting an “Empty pipe error,” so I moved it just inside the last bracket. The CSV get created, but no data. I have attached the new code and changes. Where might I be going wrong.

$ADGroups = Get-Content "C:\Scripts\VDIUSage\VDIGroups.txt"
#Get-ADGroup -Filter * <#{ name -like "*VDI*" }#> -SearchBase "OU=VDI,OU=Application,OU=GROUP,DC=GENPT,DC=NET" -Properties info, ManagedBy |
#Get-ADGroup -Identity  -Properties info, ManagedBy |
Foreach ($ADGroup in $ADGroups) { 
    Get-ADGroup -Identity $ADGroup -Properties info, ManagedBy |
          ForEach-Object {
            $Group = $_
            Get-ADGroupMember -Identity $_.objectguid |
                Where {$_.objectclass -eq 'user'} |
                    ForEach-Object {
                        [PSCustomObject]@{
                            AD_GroupName     = $Group.name
                            AD_Group_Notes   = $Group.Info
                            AD_Group_Manager = $(Get-ADUser -Identity $($Group.managedby) -Properties cn).cn
                            Name             = $_.name
                            sAMAccountName   = $_.samaccountname
			                Email			 = $(Get-ADUser -Identity $($_.SamAccountName) -Properties EmailAddress).emailaddress
                            Department       = $(Get-ADUser -Identity $($_.SamAccountName) -Properties Department).Department
                                }
                        }
             } | export-csv -Path "c:\scripts\VDIGroupMem.csv" -NoTypeInformation
         }

Simply assign the “output” from the foreach loop to a variable and pipe this to Export-Csv:

$Result =
Foreach ($ADGroup in $ADGroups) { 
    Get-ADGroup -Identity $ADGroup -Properties info, ManagedBy |
    ForEach-Object {
        $Group = $_
        Get-ADGroupMember -Identity $_.objectguid |
        Where-Object { $_.objectclass -eq 'user' } |
        ForEach-Object {
            [PSCustomObject]@{
                AD_GroupName     = $Group.name
                AD_Group_Notes   = $Group.Info
                AD_Group_Manager = $(Get-ADUser -Identity $($Group.managedby) -Properties cn).cn
                Name             = $_.name
                sAMAccountName   = $_.samaccountname
                Email            = $(Get-ADUser -Identity $($_.SamAccountName) -Properties EmailAddress).emailaddress
                Department       = $(Get-ADUser -Identity $($_.SamAccountName) -Properties Department).Department
            }
        }
    } 
}

$Result | 
Export-Csv -Path 'c:\scripts\VDIGroupMem.csv' -NoTypeInformation

That worked awesome. One last thing and I am done. With the parameters, how can I test for null/blank? If they are blank, how can I set the value to “No Value” (for sorting and reporting)?

It depends on the type of the property. But because most of the proerties are strings you can use the .IsNullOrEmpty() method.