Get members of Local Admin Account

I am try to get members of Local Administrators Account into PSobject, which I would like to use later , Below is the function not sure what is wrong… Not able to get it right.

function Get-InfoLocaladmin{
$admins = Get-WmiObject -Class win32_groupuser -ComputerName $ComputerName | Where-Object {$_.groupcomponent –like ‘*“Administrators”’}
foreach ($admin in $admins){
$props = @{$admin.partcomponent –match “.+Domain=(.+),Name=(.+)$” > $null
$matches[1].trim(‘"’) + “\” + $matches[2].trim(‘"’)}
New-Object -TypeName PSObject -Property $props

Well, $nul is a typo - did you maybe mean $null?

Keep in mind that I can’t necessarily run your code. It would be helpful if you provided more detail on “not able to get it right.”

Hi Don

I am trying to get this function working so that I can use it with your wonderful book and module on Creating HTML Reports in PowerShell

Below is the error…

At line:8 char:40

  •     $props = @{$admin.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $ ...
  •                                    ~

Missing ‘=’ operator after key in hash literal.
At line:10 char:12

  •     New-Object -TypeName PSObject -Property $props
  •        ~

Missing ‘=’ operator after key in hash literal.
At line:7 char:32

  • foreach ($admin in $admins){
  •                            ~

Missing closing ‘}’ in statement block.
At line:1 char:28

  • function Get-InfoLocaladmin{
  •                        ~

Missing closing ‘}’ in statement block.
+ CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral

Hi Preenesh,

PowerShell complains about your hashtable which isn’t one because you’re trying to match a regular expression, pipe to $null, and don’t provide proper key value pairs. Please find a fixed version below:

function Get-InfoLocaladmin
    $admins = Get-WmiObject -Class win32_groupuser -ComputerName $ComputerName | Where-Object {$_.groupcomponent –like '*"Administrators"'}
    foreach ($admin in $admins)
        if ($admin.partcomponent –match '.+Domain="(.+)",Name="(.+)"$')
            $props = @{
                DomainName = $Matches[1]
                UserOrGroupName = $Matches[2]
            New-Object -TypeName PSObject -Property $props


Yes that was $null… but still have these errors… Have checked and double checked but dont see any missing closing }

Please help !!

Thanks Daniel… Understood what I was doing wrong

Thanks again…!!

If .NET 3.5 is installed, you can use the System.DirectoryServices.AccountManagement APIs to get strongly-typed objects for the group and its members.

Add-Type -AssemblyName 'System.DirectoryServices.AccountManagement'

$groupCtx = New-Object 'DirectoryServices.AccountManagement.PrincipalContext' ([DirectoryServices.AccountManagement.ContextType]::Machine)
[DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity( $groupCtx, 'Administrators' )

Here is a nifty looking script that may be able to provide some answers for you:

It looks like it gets more than just members of the local admin group too