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: Send Private Message to Another Computer

$name = read-host "Enter remote computer name "$messageInput = read-host "Enter your message to send…

PowerShell: Simulate File Open Locking

# Simulate error by creating file in current directory with some contents; then, open file…

PowerShell: Grant Domain Admins Access to Directories

Snippet: # Messing around$shareDrives=@("E","F","G","I","J","N",'O','Q','R','S','T','U','V','W','Z','Y');$subdomain=(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1';$domainadmins="$subdomain`\Domain Admins"; $shareDrives | %{"Add-NTFSAccess…