Posted On October 28, 2021

PowerShell: Add User To Group in Active Directory

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Add User To Group in Active Directory
$userId='kimconnect'
$groupName='Remote Desktop Users'

function addUserToGroup($userId,$groupName){
  $userExists=Get-ADGroupMember $groupName|?{$_.SamAccountName -eq $userId}
  if(!$userExists){
      Add-ADGroupMember -Identity $groupName -Members $userId
  }else{
      write-host "User ID '$userId' already exists in group '$groupName'"
  }
}

addUserToGroup $userId $groupName

Leave a Reply

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

Related Post

PowerShell: Generate Random Password

Previous version that does not require System.Web assembly: https://blog.kimconnect.com/powershell-randompassword-function/ # This function will quickly generate…

PowerShell: DHCP Server Migration

Nowadays, being lazy is good. Why waste energy with clicking buttons when there are a…

Basic CSS: Use RGB values to Color Elements

<style>body {background-color: #F00;}</style>