Friday, April 12, 2013

Fiddling around with the ActiveDirectory PowerShell module part 1

It has been a few weeks since my last post and i feel obliged to blog something today. so i think the Active Directory module is worth mining. today a simple PowerShell script to list User membership in the AD.

since the AD cmdlets are very extensive it took me a little while to find out how to get group memberships out of the AD. This little script will show the membership of a user from a AD group and it will save it into a Excel workbook.



Import-Module ActiveDirectory
$a = New-Object -ComObject excel.application
$a.visible = $true
$b = $a.workbooks.add()
$C = $b.ActiveSheet

$c.Cells.Item(1,1) = "Login ID"
$c.Cells.Item(1,2) = "Member Off"
$i = 2
$group = "Enterprise Admins","Domain Admins"

foreach ($grp in $group)
{$users = Get-ADGroupMember $grp
foreach($_ in $users){
$c.cells.item($i,1) = $_.SamAccountName;$c.cells.item($i,2) = $grp;$i=$i+1
}
}
$b.SaveAs("C:\Powershell\Adminsreport\Admins.xls")
$a.quit()

No comments:

Post a Comment