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…

Notable Features of C#

This language is part of the .NET framework that can be used to build applications…

PowerShell: Replacing Characters Inside Text Files

$textFilesDirectory="C:\Users\$env:computername\Desktop\test" $textFileExtensions='.txt' $regexNewlineOnly="(?<!\r)\n$" $replaceWith="`r`n" function updateFile{ param( $file="C:\Users\$env:username\Desktop\test.txt", $regexMatch="(?<!\r)\n$", $replaceWith="`r`n", $output ) $output=if($output){$output}else{$file} if($file -ne…