I need to audit our local administrators group. I wanted to convert my script to Powershell that I’ve used for years. I found the magic post here that shows the core syntax. I wouldn’t have guessed the syntax in a dozen years.
Here is the VBScript.
Set objGroup = GetObject(“WinNT://./Administrators,group”)
For Each objUser In objGroup.Members
WScript.Echo “Member found: ” & objUser.Name
Next
set objGroup = Nothing
Here is the Powershell syntax.
function LogToFile ([string]$strFileName, [string]$strComputer)
{
Add-Content $strFileName $strComputer
}
$strComputer = “.”
$computer = [ADSI](“WinNT://” + $strComputer + “,computer”)
$Group = $computer.psbase.children.find(“Administrators”)
$members= $Group.psbase.invoke(“Members”) | %{$_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)}
ForEach($user in $members)
{
Write-Host $user
$a = $strComputer + “!” + $user.ToString()
LogToFile “C:ss.txt” $a
}
Thanks to Ying Li!
Cheers,
Steve
5 Comments
Gustavo said
Thanks for saving my life with this 🙂
where did you get the "psbase" from? I didn't find this approach in any other site.
Anyway, thanks again.
Yasser said
Hi,
how can I run this for multiple computers and save the oupt to a txt file or csv? I also like to repeat the machine name on every line. Thank you
Yasser
nalugoju said
Hi ,
iam new to powershell can any one tell me how to create user in localadmin group not in domain
Troy said
Oh goodie, a list of group names that we still have to go locate and enumerate. You've saved us about 10% of our work here.
admin said
Hi Troy,
Glad it helped (I think). 🙂