Virtual Geek

Tales from real IT system administrators world and non-production environment

Oneliner Powershell How to Add and Remove AD users and Objects in Groups members

This is one-liner simple and small active directory powershell script to add or remove AD groups members from text file or a single user, I have already written another few articles in this regards as you can find below.

Installing, importing and using any module in powershell
Powershell one liner: Create multiple user accounts
Oneliner Microsoft Powershell Script Get members from a list of group from Active Directory in excel

To start with, I have created one text file named users.txt file, it has AD users list which I will add in bulk on AD group members using below script.

Get-ADGroup <AD Group Name> | Add-ADGroupMember -Members (cat <filepath>)

active directory powershell Get-ADGroup Add-ADGroupMember -members domain add group members on ad group automation devops.png

Below is a common command to verify each time when user objects is/are added or removed in the group members.

Get-ADGroupMember <AD Group Name> | Select-Object Name

If a single user need to added simply use below command, replace filename with single username.

Get-ADGroup <AD Group Name> | Add-ADGroupMember -Members <AD User Name>

powershell microsoft active directory script automation get-adgroupmember devops select-object name add-adgroupmember get-adgroupmember remove-adgroupmember confirm no prompt.png

Above all the commands where related to adding users to the group, Next commands are all related to removing users, from text files as well as single user.

Get-ADGroup <AD Group Name> | Remove-ADGroupMember -Members (cat <FilePath>) -Confirm:$false
Get-ADGroup <AD Group Name> | Remove-ADGroupMember -Members <AD User Name> -Confirm:$false

Useful Articles
Active Directory Powershell: Create bulk users from CSV file
Active Directory Powershell: Aduser A value for the attribute was not in the acceptable range of values
Powershell Active Directory: ADGroup Managedby - Checkbox Manager can update membership list
Microsoft Active directory additional features - AD Recycle Bin Powershell

Go Back


Blog Search

Page Views


Follow me on Blogarama