Add Computers to AD Group
Here is a simple little script for adding a mass number of machines to an AD Group.
Note: You will need to make sure you have the AD Powershell module installed on the machine you run this on.
Import-Module ActiveDirectory
$ADGroup = "AdGroup"
$Members = Get-ADGroupMember -Identity $ADGroup
$Machines = get-content "E:\Powershell Script\AddMachinesToADGroup.txt"
$Failures = @()
$Added = @()
$Existing = @()
Foreach ($Machine in $Machines)
{
if ($Machine -in $Members.SAMAccountName)
{
$Existing += $Machine
}
else
{
try {
$Machine += "$"
"Trying to Add $($Machine)"
Add-ADGroupMember -Identity $ADGroup -Members $Machine
$Added += $Machine
}
catch { $Failures += $Machine }
}
}
Note: You will need to make sure you have the AD Powershell module installed on the machine you run this on.
Import-Module ActiveDirectory
$ADGroup = "AdGroup"
$Members = Get-ADGroupMember -Identity $ADGroup
$Machines = get-content "E:\Powershell Script\AddMachinesToADGroup.txt"
$Failures = @()
$Added = @()
$Existing = @()
Foreach ($Machine in $Machines)
{
if ($Machine -in $Members.SAMAccountName)
{
$Existing += $Machine
}
else
{
try {
$Machine += "$"
"Trying to Add $($Machine)"
Add-ADGroupMember -Identity $ADGroup -Members $Machine
$Added += $Machine
}
catch { $Failures += $Machine }
}
}
Comments
Post a Comment