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

How To Stop, Start, Restart a Windows Service Being Stuck in ‘Stopping’ Status

Convenient Function:(related to https://blog.kimconnect.com/powershell-kill-a-windows-service-forcefully) # forceRestartService.ps1 # version 0.0.2 $serviceName='Spooler' function forceRestartService($serviceName='Spooler'){ $processId=Get-WmiObject -Class Win32_Service…

PowerShell: Find Process ID (PID) Locking a Certain File

Here's a function to kill common processes (Antivirus executable excluded) locking a particular file. This…

PowerShell: Recover Deleted Active Directory Objects

$domain="kimconnect"$ltd="com"$dc="dc01"$userToRecover="Tom Cruise"# Enable Active Directory Recycle Bin# Enable-ADOptionalFeature 'Recycle Bin Feature' -Scope ForestOrConfigurationSet -Target "$domain.$ltd"…