I am a powershell newbie (on V3). I am trying to find a way to retrieve the members of the local admin group on a remote machine. Below is the closest command I could find to retrieve such information (other than the alternate methods of using some ADSI queries which I am not familiar with). Using the “GroupComponent” property of the below output object, I should be able to get the desired list (after the text manipulations).
My query is,
Below command works fine, takes about 10 seconds to run on a workgroup/domain machine.
get-wmiobject -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$env:computername',Name='administrators'"""
I was trying to construct the similar command using the powershell way (instead of query lang) as seen below.
Get-WmiObject -Class win32_groupuser | where pscomputername -Match $env:COMPUTERNAME | where GroupComponent -Match win32_group.domain
Both commands works absolutely fine on a workgroup machine. But when I use them on a domain machine, latter just takes too much of time and never completes (it does not return any error, which means the command/syntax is fine).
What am I doing wrong? If “Get-WmiObject -Class win32_groupuser” is retrieving too much of details (inefficient filtering) from AD which is slowing down the process, why is it not happening on the 1st command? How can I make the 2nd style of command working?