Last active
January 6, 2022 22:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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