Powershell Crushes VBScript with AD query again
In my on going battle on EE to prove the value of powershell, I have destroyed yet another vbscript. While the vbscript look was impressive it was a monsterous 38 lines compaired to Powershell 12. Again with no short-cuts, you could trim another 4 lines with ease.
Here is my code. It gets all the groups and list the group name / member count / OU.
$root = [ADSI]""
$dsSearcher = new-Object System.DirectoryServices.DirectorySearcher($root,$filter)
$dsSearcher.PageSize = 1000
$groups = $dsSearcher.findAll()
@(foreach($group in $groups)
{
[string]$name = $group.Properties.cn
[string]$count = ($group.psbase.properties.member).count
[string]$OU = ((($group.GetDirectoryEntry()).psbase).parent).distinguishedName
"GROUP:{0} Count:{1} OU:{2}" -f $name.PadRight(35),$count.padright(5),$ou
}) | out-file C:\temp\yourfile.txt -enc ASCII
Output looks like this
GROUP:TGroup1 Count: OU:OU=MyGroups,DC=corp,DC=bb,DC=lab
GROUP:TGroup2 Count: OU:OU=MyGroups,DC=corp,DC=bb,DC=lab
GROUP:TGroup3 Count: OU:OU=MyGroups,DC=corp,DC=bb,DC=lab
GROUP:TGroup4 Count:1 OU:OU=MyGroups,DC=corp,DC=bb,DC=lab
GROUP:TGroup5 Count:2 OU:OU=MyGroups,DC=corp,DC=bb,DC=lab
….
tshell :: Aug.31.2007 :: Active Directory, Powershell, Scripting :: 3 Comments »

