Powershell Script to Add Multiple computers to a Security Group
In most enterprise environments, managing security groups within Active Directory (AD) is a critical part of day-to-day administration. Security groups are often used to grant access to resources, apply group policies, or push software deployments in a controlled manner. While adding a single computer to a group is straightforward, things can quickly become tedious when you need to update dozens or even hundreds of machines at once.
Consider a few common scenarios:
- You’re implementing a new Organizational Unit (OU) structure and need to ensure that all machines within a specific department are added to the right security group.
- Your IT team is preparing for a software rollout (for example, deploying antivirus, productivity tools, or custom applications) and you must enable group-based targeting.
- You’re applying restricted access policies or adjusting permissions across multiple endpoints in response to a security audit or compliance requirement.
In each of these cases, the manual approach—searching for every computer object in Active Directory and adding it individually—can be both time-consuming and prone to errors. This is where automation comes to the rescue.
The PowerShell script provided below is designed to simplify bulk computer management. Instead of spending hours making changes one by one, this script allows you to add multiple computers to a designated security group with just a single click. It not only reduces administrative overhead but also ensures accuracy and consistency across your environment.
By leveraging automation in scenarios like this, IT administrators can free up valuable time, reduce repetitive tasks, and focus on more strategic initiatives. Whether you’re managing a handful of machines or scaling operations across hundreds of systems, this approach will help you streamline your workflow and maintain better control over your Active Directory infrastructure.
# Path to text file with computer names $ComputerList = Get-Content -Path "C:\Temp\computers.txt" # File which will feed the computer names to the Script # AD group where the computers should be added $GroupName = "CN=ComputersGroup,OU=SecurityGroups,DC=Domain,DC=com" #Distinguished name of the Group to which computer needs to be added foreach ($Computer in $ComputerList) { try { # Get the computer object $ComputerObj = Get-ADComputer -Identity $Computer -ErrorAction Stop # Add to the group Add-ADGroupMember -Identity $GroupName -Members $ComputerObj Write-Host "Successfully added $Computer to $GroupName" } catch { Write-Host "Failed to add $Computer. Error: $($_.Exception.Message)" -ForegroundColor Red } }