I'm new to scripting and need a little help.
The script below (my first) is getting all members from a supplied AD Group, displaying the DisplayName and Name, sorting it by Name and Exporting it to a CSV file and it works nicely.
Been banging my head for the last 2 days trying to add a couple of other things to the CSV file and nothing is working. Any help in adding these last 2 options would really help my head.
1. A header to the top of the CSV file with the name of the AD Group I'm searching and also a current date
2. An additional column displaying the actual "Group/Child Group" the user is a member of.
# Get the GroupName
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$GroupName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the Group Name", "Group Name", "")
# variables
$path = "C:\ADGroups\"
# Check for Output Folder
If(-not(Test-Path $path))
{New-Item -ItemType directory $path}
# Check if there is an existing Group file
If(Test-Path $path$groupname.csv)
{Get-ChildItem $path -Include $Group.csv | Remove-Item}
# Check if GroupName exsists in AD
If (get-adgroup -Filter {SamAccountName -eq $GroupName})
# Export Header Info, Get Member list and export to CSV file
{
get-adgroupmember $groupname -recursive | where-object –FilterScript {
$_.objectClass –eq “user”} |
get-aduser -property * | select-object DisplayName,Name | Sort Name | Export-CSV -path $path$groupname.csv -NoTypeInformation
}
Else
{
Write-Warning "$GroupName does not exist in Active Directory."
}
What I get:
DisplayName Name Young Bob BYoung Johnson Joe JJohnson Taylor June JTaylor Woods Karen KWoods Marsh Tom TMarsh
What I'd like to get:
Administration 12/17/2013 16:32 AD Group DisplayName Name Administration Young Bob BYoung CEOUsr Johnson Joe JJohnson CFOUsr Taylor June JTaylor Administration Woods Karen KWoods Administration Marsh Tom TMarsh