How to Retrieve Local Administrators From Power Shell

There will be times when you need to figure out what accounts have Local Administrator permissions on a server or workstation.

I came across a Power Shell function that does it nicely.

function Get-LocalAdmin {
    param ($strComputer)

    $admins = Gwmi win32_groupuser -computer $strComputer
    $admins = $admins |? ($_.groupcomponent -like '*"Administrators"'}

    $admins |% {
        $_.partcomponent -match ".+Domain\=(.+)\,Name\=(.+)$" > $nul
        $matches[1].trim('"') + "\" + $matches[2].trim('"')

You can use the function like so…

PS C:\Users\Administrator> Get-LocalAdmin localhost
SU\Domain Admins
[cut for brevity]
Apr 16th, 2012 • Posted in Management, Windows
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>