I've found a script that I'm using to discover all members and nested members of a distribution group. The script is great, but I can't seem to find out how to export the tree view results to .txt or .csv
Any ideas?
Props to Hugo Peeters for the script!
###################################
# Get-MyGroupMembersRecursive.ps1 #
# Created by Hugo Peeters #
# http://www.peetersonline.nl #
###################################
param($ParentGroupNames)
$ParentGroupNames = 'xxxxxx'
$Global:myCol = @()
function Indent
{
param([Int]$Level)
$Global:Indent = $null
For ($x = 1 ; $x -le $Level ; $x++)
{
$Global:Indent += "`t"
}
}
function Get-MySubGroupMembersRecursive
{
param($DNs)
ForEach ($DN in $DNs)
{
$Object = Get-QADObject $DN
If ($Object.Type -eq "Group")
{
$i++
Indent $i
Write-Host ("{0}{1}" -f $Indent,$Object.DisplayName) -ForegroundColor "red"
$Group = Get-QADGroup $DN
If ($Group.Members.Length -ge 1)
{
Get-MySubGroupMembersRecursive $Group.Members
}
$i--
Indent $i
Clear-Variable Group -ErrorAction SilentlyContinue
}
Else
{
$userfound = Get-QADUser $DN | Select Name, Email
Write-Host ("{0} {1}" -f $Indent,$userfound.Name)
$Global:myCol += $userfound
Clear-Variable userfound -ErrorAction SilentlyContinue
}
}
}
ForEach ($ParentGroupName in $ParentGroupNames)
{
$Global:Indent = $null
$ParentGroup = Get-QADGroup -Name $ParentGroupName
Write-Host "====================="
Write-Host " TREE VIEW PER GROUP"
Write-Host "====================="
Write-Host ("{0}" -f $ParentGroup.DisplayName) -ForegroundColor "Blue"
If ($ParentGroup -eq $null)
{
Write-Warning "Group $ParentGroupName not found."
break
}
Else
{
$FirstMembers = $ParentGroup.Members
ForEach ($member in $firstmembers)
{
Get-MySubGroupMembersRecursive $member
}
}
}
Write-Host ""
Write-Host "====================="
Write-Host " All Unique Members: "
Write-Host "====================="
$myCol | Sort Name | Select Name, Email -Unique