Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Last active January 6, 2022 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhorsman/29e2e4cb3169b58fe1b558572cfba78b to your computer and use it in GitHub Desktop.
Save jhorsman/29e2e4cb3169b58fe1b558572cfba78b to your computer and use it in GitHub Desktop.
Add a user to a local Windows user group with PowerShell. Inspired by https://mcpmag.com/articles/2015/05/28/managing-local-groups-in-powershell.aspx
$Computer = $env:COMPUTERNAME
$GroupName = 'Topology Manager Administrators'
$ADSI = [ADSI]("WinNT://$Computer")
$Group = $ADSI.Children.Find($GroupName, 'group')
Write-Host ("Found group '{0}'" -f $Group.Name.Value)
#$User = $env:USERNAME
#$Group.Add(("WinNT://$Computer/$User"))
$User = "domain\username"
$ntUser = "WinNT://" + $User.Replace('\', '/')
$Group.Add(($ntUser))
Write-Host ("Added user '{0}' to group '{1}'" -f $User, $Group.Name.Value)
Write-Host ("'{0}' group members:" -f$Group.Name.Value)
$Group.psbase.invoke('members') | ForEach {
Write-Host (" {0}"-f $_.GetType().InvokeMember("Name","GetProperty",$Null,$_,$Null))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment