PowerShell: Quickly Add Users into Groups on a List of Computers

$newVmNames=@(
    'SERVER0001', 
    'SERVER0002'
)
$newUsers=@(
    'domain\user1'
)
$groupNames='Administrators','Remote Desktop Users'

$newVmNames|%{
    invoke-command -computername $_ {
        param($newUsers,$groupNames)
        try{            
            $groupNames|%{
                Add-LocalGroupMember -Group $_ -Member $newUsers
                write-host "$env:computername`: Group name '$groupName' now has these users`r`n$(get-localgroupmember $groupName)"
            }
        }catch{
            write-warning $_
        }        
    } -ArgumentList $newUsers,$groupNames
}

Leave a Reply

Your email address will not be published. Required fields are marked *