Saturday, 29 July 2023

PowerShell Active Directory Command

PowerShell Active Directory Command


Get User Properties
Get-ADUser UserName -Properties * | Select *

Get-ADUser UserName -Properties * | select -Property whenChanged

Add the User to the Group
Add-ADGroupMember -Identity "GroupNAME" -Member USERNAME


Add the Multiple Users to the Group
Add-ADGroupMember -Identity "GroupNAME" -Member USERNAME1, USERNAME2, USERNAME3, USERNAME4, USERNAME5, USERNAME6

Get-ADGroupMember –Identity GroupName

Get –ADGroup -Filter *
Get-ADGroup -Filter * | select name

Remove ADGroup

Remove-ADGroup –Identity GroupName

Get-ADGroupMember “GroupName


Adding a User to Multiple Groups

GroupName1","GroupName2" | Add-ADGroupMember -Members `
 (Read-Host -Prompt "Enter User Name")


Adding Users to a Group from a CSV file


If you want to add a large number of users to a group, you can specify them in a CSV file and then import that file. Note that the list of the usernames in the CSV file must contain the SamAccountNames in the users column, as shown below:


To add users to group from a CSV file, run the following PowerShell script:

Import-Csv -Path “c:\scripts\users.csv” | ForEach-Object {Add-ADGroupMember -Identity “GroupName” -Members $_.’Users’}

Bulk Remove Group Users

Import-Csv -Path “C:\scripts\users.csv” | ForEach-Object {Remove-ADGroupMember -Identity “Group-Name” -Members $_.’Users’ -Confirm:$false}

Removing Users or Computers from a Group

Remove-ADGroupMember –Identity GroupName –Members UserName

Removing a User from All Groups

To remove a user from all groups, run this script:
Username = A
Get-ADUser -Identity A -Properties MemberOf | ForEach-Object {
 $_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false



No comments:

Post a Comment