Skip to content

Instantly share code, notes, and snippets.

@HarmJ0y
Last active April 27, 2023 13:56
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save HarmJ0y/fd98c4f16575ba28c091 to your computer and use it in GitHub Desktop.
Save HarmJ0y/fd98c4f16575ba28c091 to your computer and use it in GitHub Desktop.
Powershell ADSI tricks
# Add a domain user to a remote server local group, if your current user has admin over the remote machine
powershell -c ([ADSI]'WinNT://SERVER/Administrators,group').add('WinNT://DOMAIN/USER,user')
# Get all local groups on a remote server
powershell -c "([ADSI]'WinNT://SERVER,computer').psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach { ($_.name)[0]}"
# Find members of the local Administrators group on a remote server
powershell -c "$([ADSI]'WinNT://SERVER/Administrators,group').psbase.Invoke('Members') | foreach { $_.GetType().InvokeMember('ADspath', 'GetProperty', $null, $_, $null).Replace('WinNT://', '') }"
# Enable the local Administrator account on a remote server
powershell -c "$a=([ADSI]'WinNT://SERVER/Administrator,user');$a.UserFlags=2;$a.CommitChanges()"
# Disable the local Administrator account on a remote server
powershell -c "$a=([ADSI]'WinNT://SERVER/Administrator,user');$a.UserFlags=512;$a.CommitChanges()"
@potatoqualitee
Copy link

HEY THANKS HARMJ0Y, I needed this and stumbled upon you ;)

@AFineDayFor
Copy link

I also needed to stumble upon this, thanks :)

@jedilibrarian
Copy link

Found out how to do some of these earlier today but not all. Useful stuff thank you for sharing.

@vxjg67
Copy link

vxjg67 commented Jun 20, 2019

Stumbled here too - very handy. In addition to the 'local group' one, here is how to make the computer a variable:

$comp = "remotemachine"

$ADSIComputer = [ADSI]("WinNT://$comp,computer")
$ADSIComputer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach { ($_.name)[0]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment