Posted On November 22, 2021

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

PowerShell: Check RPC Reachability of Remote Computer

# This is an ancient script that would work with PowerShell versions 2 to 5,…

HTML: Tagging and Jumping to Sections of Page

Each element of an HTML document could be set with a unique ID so that…

JavaScript Challenge: Counting Words

This is useful to render word clouds, emphasizing more commonly occurring words in a database.…