Pages

Monday, July 6, 2015

Add list of users to AD group from .csv file

This script will use a .csv file containing a list of usernames that need to be made members of an AD group.
The first cell in the column containing the usernames must use the value “Name”.

$ListOfUsers = Import-csv -Path <INSERT PATH TO .CSV FILE>
$ItemDetails = $NULL

foreach ($item in $ListOfUsers)
{
$ItemDetails = get-aduser -Identity $item.name -properties * -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($ItemDetails -eq $NULL)
    {
    Write-Host $item.name "does not exist in AD"  -ForegroundColor Red
    $ItemDetails = $NULL
    }
else
    {
    Write-Host $item.name  "does exist in AD" -ForegroundColor Green
    $ItemDetails.DistinguishedName
    $GroupToAddTo = get-adgroup -Identity "<INSERT GROUP NAME>"
    Add-ADGroupMember -Identity $GroupToAddTo -Member $ItemDetails.DistinguishedName
    $ItemDetails = $NULL
    }
}

No comments:

Post a Comment