Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a column to a Select output

Status
Not open for further replies.

g33man

Programmer
Dec 22, 2006
50
0
6
CA
Below is my script (borrowed from the Internet). It produces a report of all the users in each group.

Code:
Import-Module ActiveDirectory
$groups= "GROUP1","GROUP2","GROUP3"
$selectgroups=$groups |Get-Adgroup
$selectgroups |get-adgroupmember -Recursive |Get-ADUser -property sn |Select givenname,sn,userPrincipalName

What I need to do is create a 4th column which is the actual group name (from $groups) that the user came from.

Could this be a clever tweak to my script?
With thanks,
Mike
 
How about something like this?

Code:
Import-Module ActiveDirectory
$groups= "GROUP1","GROUP2","GROUP3"
$memberships = @()
foreach ($group in $groups)
	{
	 $members = Get-ADGroupMember $group -Recursive
	 foreach ($member in $members)
		{
		 $user = Get-Aduser $member -Properties sn
		 $record = New-Object PSObject
		 $record | Add-Member -Type NoteProperty -Name GivenName -Value $user.GivenName
		 $record | Add-Member -Type NoteProperty -Name Surname -Value $user.sn
		 $record | Add-Member -Type NoteProperty -Name UserPrincipalName -Value $user.UserPrincipalName
		 $record | Add-Member -Type NoteProperty -Name ParentGroup -Value $group
		 $record
		 $memberships += $record
		}
	}


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top