This powershell creates a function called "Show-ADGroup" with two parameters to display the members of an AD group.
#
# Show-ADGroup - show members of an AD group, exports to out-grid or CSV on desktop.
#
# Parameters:
# Gname - name of AD group to show
# ToFileYN - "Y" will show export list to CSV file; otherwise, display in Out-gridview
#
function Show-ADGroup
{
param (
[Parameter(Mandatory)] $Gname,
[Parameter(Mandatory)] $TofileYN
)
# set $DCname to one of your local domain controllers
$DCname = "your_DC_name"
# counts number of members
$cnt = (Get-ADGroup $Gname -Properties *).Member.Count
Write-Host "# members: $cnt"
# specify location of export to file; in this case the user's desktop
$path = "C:\Users\$ENV:USERNAME\Desktop"
$pathexist = Test-Path -Path $path
If ($pathexist -eq $false)
{New-Item -type directory -Path $path}
$reportdate = Get-Date -Format ssddmmyyyy
$csvreportfile = $path + "\ADGroupMembers_$reportdate.csv"
if ($TofileYN -eq "Y") {
Get-ADGroupMember -Identity "$Gname" -Server $DCname|
Select-Object @{Label = "Name";Expression = {$_.Name}},
@{Label = "SamAcctName";Expression = {$_.SamAccountName}},
@{Label = "distinguishedName";Expression = {$_.distinguishedName}} |
Export-Csv -Path $csvreportfile -NoTypeInformation
} else {
Get-ADGroupMember -Identity "$Gname" -Server $DCname|
Select-Object @{Label = "Name";Expression = {$_.Name}},
@{Label = "SamAcctName";Expression = {$_.SamAccountName}},
@{Label = "distinguishedName";Expression = {$_.distinguishedName}} |
Out-GridView
}
}
No comments:
Post a Comment