Explore

Our Services

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Web Development

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do etc.

Database Analysis

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do etc.

Server Security

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do etc.

image
image
image
image
Explore

Design & Development

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

image

DO YOU HAVE ANY PROJECT ?

Let’s Talk About Business Soluations With Us

image
image
image
image
image
image
image
image
image
Powershell – Add Active Directory User to a Group

Start transcript

Start-Transcript -Path C:\Temp\Add-ADUsers.log -Append

Import AD Module

Import-Module ActiveDirectory

Import the data from CSV file and assign it to variable

$Users = Import-Csv “C:\Temp\Users.csv”

Specify target group name (pre-Windows 2000) where the users will be added to

You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local

Import-Csv "C:\Temp\Users.csv"

$Group = “Pilot”

foreach ($User in $Users) {
# Retrieve UPN
$UPN = $User.UserPrincipalName

# Retrieve UPN related SamAccountName
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName

# User from CSV not in AD
if ($ADUser -eq $null) {
    Write-Host "$UPN does not exist in AD" -ForegroundColor Red
}
else {
    # Retrieve AD user group membership
    $ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name

    # User already member of group
    if ($ExistingGroups.Name -eq $Group) {
        Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
    }
    else {
        # Add user to group
        Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -WhatIf
        Write-Host "Added $UPN to $Group" -ForeGroundColor Green
    }
}

}
Stop-Transcript

  • Line 8: Edit the CSV file name and the path.
  • Line 12: Edit the target group.

Credit – https://www.alitajran.com/add-users-to-group-powershell/

image
image
image
image
image
image
image
image