Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Group Membership Audit

$
0
0

I'm trying to get a list of group members for selected groups. It's for groups that start with "RPT-" and this is what I have so far.

I did not get a chance to clean it up yet, because I'm trying to refine it first but here it is:

Import-Module ActiveDirectory
cd AD:
#creates initial CSV File
$MemberList = New-Item -Type file -Force “C:\Reports\GroupMembers.csv”
#Gets list of needed groups and exports them to Excel
Get-ADGroup -filter 'Name -like"rpt*"' | Select-Object SamAccountName | Export-csv C:\reports\rptgroups.csv
#declares the group csv as the input groups
$inputfile="C:\reports\rptgroups.csv"
#removes the default first line put by the group look above
Get-Content $inputfile | ? { $_ -notlike '*#type*' } | Set-Content "$inputfile-temp"
move "$inputfile-temp" $inputfile -Force
#imports the CSV and lists each member
Import-Csv $inputfile | ForEach-Object {
	$GName = $_.Samaccountname
	$group = Get-ADGroup $GName 
	$group.Name + "`n" | Out-File $MemberList -Encoding Unicode -Append
		foreach ($member in Get-ADGroupMember $group) {$member.Name | Out-File $MemberList -Encoding Unicode -Append}
$nl = [Environment]::NewLine | Out-File $MemberList -Encoding ASCII -Append
}

This is the results that I get. (The highlighted name being the group name)

As you can see, each of the group names after the main one gets a ?? added before it.

I'm sure there is probably a better way to do this (the whole thing). I'm looking for any suggestions to make this cleaner/efficient.

Thanks,

Josh


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>