Managing Active Directory (AD) groups manually can be tedious and error‑prone, especially when dealing with large numbers of users or computers. With PowerShell automation, administrators can quickly bulk‑add accounts to AD groups using a simple script and a CSV file. This guide walks you through the process step‑by‑step, showing how to import users or computers, run the script, handle errors, and verify group membership—all while saving time and ensuring consistency across your environment.
How to Use Script
See the example below. To view full help, type the script name and press Enter. When adding users or computers to an Active Directory group, you must provide either the computer NetBIOS name or the user’s SAM account name in a text file that the script can read.
Add computer to AD Group
.\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType Computer
Add user to AD group
.\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType User
PowerShell Script
#Version: 1.0
#Author: Equebal Ahmad
<#
.SYNOPSIS
Add the computers / users account to an AD Group
.DESCRIPTION
Add the computers / users account to an AD Group
The script can add user and computer in an AD group. You need to pass this with ObjectType parameter.
.EXAMPLE
.\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType Computer
.EXAMPLE
.\AddObjectsToADGroup.ps1 -FileName Computers.txt -ADGroupName "TestGroup" -ObjectType User
.NOTES
None
.PARAMETER FileName
Provide the name of text file with list of computers / users to be added to AD Group
.PARAMETER ADGroupName
Name of AD group where computers / users to be added
.PARAMETER ObjectType
The ObjectType can be either computer or user. Any other parameter will not accepted.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$FileName,
[Parameter(Mandatory=$True)]
[string]$ADGroupName,
[Parameter(Mandatory=$True)]
[ValidateSet('Computer','User')]
[string]$ObjectType
)
#Import modules
Import-Module ActiveDirectory
#Delcare variables
$TargetGroup = Get-ADGroup -Filter {Name -eq $ADGroupName}
$TargetGroup
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$input = $directorypath + "\" + $FileName
if($ObjectType -eq "Computer"){
Get-Content $input | ForEach-Object{
$computer = Get-ADComputer $_
Add-ADGroupMember -identity $TargetGroup -Members $computer.DistinguishedName}}
else{
Get-Content $input | ForEach-Object {
$User = Get-ADUser $_
Add-ADGroupMember -Identity $TargetGroup -Members $User.DistinguishedName}}
Script Download
Download the script from the link below to get started.
Related Posts:
- Powershell – Get System up time
- Powershell Script : List AD Organizational Unit and GPOs linked to them
- Powershell – Compare hardware and software details on two computers
- Powershell Script : Retrieve AD Computers Properties
- Powershell – Merge CSV files & Insert file name as a column
- Powershell Script: Validate if Computer account exists in Active Directory
- Powershell remote – WinRM cannot complete the operation
- Powershell script to Add bulk users / computers to AD Group
- PowerShell Script : Copy AD Group Membership
- Using PowerShell Behind a Proxy Server
Subscribe to Techuisitive Newsletter
Be the first to know about our new blog posts. Get our newsletters directly in your inbox and stay up to date about Modern Desktop Management technologies & news.