Pages

Monday, July 6, 2015

Add list of computers to AD group from .csv file

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

$ListOfComputers = Import-csv –Path <INSERT PATH TO .CSV FILE>
foreach ($item in $ListOfComputers)
{
$ItemDetails = Get-ADComputer -identity $item.Name -properties * -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($ItemDetails -eq $NULL)
{
Write-Host "$item does not exist in AD" -ForegroundColor Yellow
$ItemDetails = $NULL 
}
else
{
$ItemDetails.DistinguishedName
$GroupToAddTo = get-adgroup -Identity "<INSERT GROUP NAME>"
Add-ADGroupMember -Identity $GroupToAddTo -Member $ItemDetails.DistinguishedName   
}
}

8 comments: