Although a large number of scripts are available already for this job, most of them do not include an option to enumerate a remote machine.
My script uses WMI, allows you to query remote machines and returns objects for future use.
Thanks to my colleague Jason for the inspiration and help with this script!
function get-localadministrators {
param ([string]$computername=$env:computername)
$computername = $computername.toupper()
$ADMINS = get-wmiobject -computername $computername -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
foreach ($ADMIN in $ADMINS) {
$admin = $admin.replace("$computernamerootcimv2:Win32_UserAccount.Domain=","") # trims the results for a user
$admin = $admin.replace("$computernamerootcimv2:Win32_Group.Domain=","") # trims the results for a group
$admin = $admin.replace('",Name="',"")
$admin = $admin.REPLACE("""","")#strips the last "
$objOutput = New-Object PSObject -Property @{
Machinename = $computername
Fullname = ($admin)
DomainName =$admin.split("")[0]
UserName = $admin.split("")[1]
}#end object
$objreport+=@($objoutput)
}#end for
return $objreport
}#end function
get-localadministrators



Can you specify a little more how I would query remote computers with this script?
Nevermind I figured it out! Also is there a way to go through each of the groups it finds and list whoever is in there?
Hi Nick, thanks for taking the time to post feedback!
I’ll look at enumerating nested groups for you later this evening.
Cheers!
A
Any luck my friend? Just wondering if you ever got the enumerating portion going?
Thanks!
Nick,
Not sure if you need this, but I needed it – this script block works for me…
$LocalGroups = gwmi win32_group|?{$_.domain -eq $env:computername}|select -ExpandProperty Name
Foreach($localGroup in $LocalGroups)
{
“Members in the $localgroup :”
$computer = [adsi](“WinNT://”+$env:COMPUTERNAME+”,computer”)
#$computer.psbase.children.find(“$localGroup”)
$group = $computer.psbase.children.find(“$localGroup”)
$group.psbase.invoke(“Members”)|%{$_.gettype().InvokeMember(“Adspath”,’GetProperty’,$null, $_, $null)}
“”
}
Everything I run is wrapped into a scriptblock and executed remotely, which is how I get away with using variables like $env:computername.
Hope this helps.
Hi Nick,
I have attempted to do this, but I’m sorry to say I don’t have the time to get you a full module to enumerate all groups.
No worries! Thanks for the attempt I appreciate it.
How would I modify this to read a text file of predefined computer names into the function get-localadministrators?
Hi Meridian,
put each server name on a line in your text file
get-content “c:blablablaservers.txt” | % {get-localadministrators $_}
Script works fine against local computer, but adding
get-content “c:pathfilename.txt” | % {get-localadministrators $_} for additional machines on a new line anywhere in the script results in errors.
hi Jim,
this works ok for me as follows:
“server1″,”server2″ | % {get-localadministrators -computername $_}
Thanks Andrew!
Just what I needed.
I’m still confused on how to query a remote computer whith this script? Can anyone explane?
Hi Chris,
Does:
Get-localadministrators -computername “computer01″
Not work?
Im having problems getting this to read from a file with a list of computers. Im new to scripting so what do i need to put where inorder to do this. I would also like to get the output to a text file.
Thanks
Hi Jim, try:
Get-content “c:file.txt” | % {get-localadministrators -computername $_}
To read from a text fie, if that works report back and ill tackle your second request.
For some reason, the script only shows the users on the local machine, not the machine indicated by the -computername parameter.
Any ideas?
David, add the following line after the function:
foreach($server in (gc .masterserver.txt)){get-localadministrators -computername $server}
Change .masterserver.txt to the path of a text file that has a list of your servers one per line.
I hate to ask this but I am an absolute beginner. Can you show exactly where to add this? I added it where what I thought was after the function but I get nothing but errors.
Add what terry? The computername?
Hi,
How to export the result to .csv
http://lmgtfy.com/?q=Export+to+csv+powershell
foreach($server in (gc .masterserver.txt)){get-localadministrators -computername $server}
Excelent PowerShell script!
You can also use this free GUI tool: “Get Local Admins GUI”
This tool permits to see the members of the local Administrators group on multiple remote computers. You can export results to CSV and import a machine list from Active Directory OU.
More info about:
http://www.sysadmit.com/2016/02/windows-buscar-administradores-locales.html