Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Export-CSV file manipulation Adding header info and AD groupname to Get-AdGroupMember

$
0
0

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:

DisplayNameName
Young BobBYoung
Johnson JoeJJohnson
Taylor JuneJTaylor
Woods KarenKWoods
Marsh TomTMarsh

What I'd like to get:

Administration
12/17/2013 16:32
AD GroupDisplayNameName
AdministrationYoung BobBYoung
CEOUsrJohnson JoeJJohnson
CFOUsrTaylor JuneJTaylor
AdministrationWoods KarenKWoods
AdministrationMarsh TomTMarsh


Viewing all articles
Browse latest Browse all 6937

Trending Articles