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 }
    }
}

Comments

Popular posts from this blog

Intune Hybrid - NDES Cert Issue

Stuck @ "Waiting for user logon"

Triggering a software update install via Powershell