PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Script blogs

Mirroring two AD groups using PowerShell (Fast)

themeanmachine19@gmail.com by [email protected]
December 14, 2021
in Powershell Script blogs
0
Mirroring two AD groups using PowerShell
Share on FacebookShare on Twitter

You might also like

Add extra dns server using powershell

How To Add Extra DNS Server Using PowerShell (2022)

July 7, 2022
Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

May 21, 2022

Hello, PowerShell enthusiast today we will understand how to mirror two AD groups using PowerShell. Sometimes back there was some urgency in my infra to mirror two different AD group users for completely different security groups and there were 1000-2000 users who needed to be checked and added. I was doing this process manually but was mistakenly adding the user who was already part of the security group.

The process took me ages to complete but let me share the script which I have created to mitigate this ridiculously easy activity.

Mirroring two AD groups using PowerShell

How to use Get-Adgroupmember?

As the name suggests Get-Adgroupmember gets the Active directory group members of the given group name.

Syntax:

				
					Get-ADGroupMember
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   [-Identity] <ADGroup>
   [-Partition <String>]
   [-Recursive]
   [-Server <String>]
   [<CommonParameters>]
				
			

Example:

We will be using the same type of script as ours.

				
					Get-ADGroupMember -Identity Administrators
distinguishedName : CN=Domain Admins,CN=Users,DC=Fabrikam,DC=com
name              : Domain Admins
objectClass       : group
objectGUID        : 5ccc6037-c2c9-42be-8e92-c8f98afd0011
SamAccountName    : Domain Admins
SID               : S-1-5-21-41432690-3719764436-1984117282-512
				
			

How to use Add-Adgroupmember?

Well, this is used to add single or multiple users to a security group.

Syntax:

				
					Add-ADGroupMember
   [-WhatIf]
   [-Confirm]
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   [-Identity] <ADGroup>
   [-Members] <ADPrincipal[]>
   [-MemberTimeToLive <TimeSpan>]
   [-Partition <String>]
   [-PassThru]
   [-Server <String>]
   [-DisablePermissiveModify]
   [<CommonParameters>]
				
			

Example:

				
					Add-ADGroupMember -Identity SvcAccPSOGroup -Members SQL01,SQL02
				
			

Find more related AD PowerShell scripts from here –> AD Scripts

Mirroring AD Groups PowerShell script

#Step 1

We need to provide the name of the groups which need to get mirrored.

				
					$group1 = read-host "Enter  group 1"
$group2 = read-host "Enter  group 2"
				
			

#Step 2

Fetching the members from the specified groups in #Step 1.

				
					$a= get-adgroupmember -identity $group1 |select -expandproperty Name
write-host "$group1 members are $a"
$b= get-adgroupmember -identity $group2 |select -expandproperty Name
write-host "$group2 members are $b"
				
			

#Step 3

We will be using a nested Foreach loop so that we can match 2 AD groups and identify what is missing in $group2 when compared to $group1.

Under if condition we will compare the results of $c and  $d with -notcontains as the name suggests it identifies what is not there in either of the groups.

Add- groupmember will be used to add the members in $group2 which are not there when compared to $group1.

Get-ADgroupmember provides the members now present in $group2.

				
					Foreach($c in $a)
{
Foreach($d in $b)
{

if ($c -notcontains $d)

{
write-host "adding $c to $group2"
add-adgroupmember -members $c -identity $group2
write-host "$c is added to $group2"
}

else
{write-host "$c is not copied"}

}
}

write-host "$group2 members are"
get-adgroupmember -identity $group2 |select -expandproperty Name
				
			

Conclusion

I hope you have liked the post Mirroring two AD groups using PowerShell and will implement this whenever it is required. Things you should always remember is to try with one more member and another group with no members so that it will be easy for you to make changes. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newsletter. 

Please login to join discussion
close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Add extra dns server using powershell

How To Add Extra DNS Server Using PowerShell (2022)

by [email protected]
July 7, 2022
0

Add extra DNS server using PowerShell. Add third DNS server IP remotely using PowerShell. Get-DnsClientServerAddress. Set-DnsClientServerAddress.

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

by [email protected]
May 21, 2022
0

Instant guide to convert ps1 to exe. How to convert ps1 to exe. PS2exe. Convert ps1 to exe using ps2exe.

PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

by [email protected]
May 15, 2022
0

Useful Powershell tips and tricks. PowerShell tips and tricks. Know Powershell tips and tricks. get-help. get-alias.

tips to manage hyper-v using powershell

5 Tips To Manage Hyper-V Using PowerShell

by [email protected]
May 1, 2022
0

Tips to manage hyper-v using PowerShell. How to manage Hyper-V using PowerShell. Powershell to manage Hyper-V. Hyper-v and PowerShell.

Next Post
Get installed software list quickly using PowerShell

Get installed software list quickly using PowerShell (2021)

Please login to join discussion

Recommended

Mirroring two AD groups using PowerShell

Mirroring two AD groups using PowerShell (Fast)

December 14, 2021
error handling in powershell

Understanding Error handling in Powershell [2021]

August 20, 2021

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    1059 shares
    Share 424 Tweet 265
  • How to find NTP Server using PowerShell?

    938 shares
    Share 375 Tweet 235
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    889 shares
    Share 356 Tweet 222
  • How to Log off Users Remotely with PowerShell (2022)

    816 shares
    Share 326 Tweet 204
  • Powershell filter: Using where-object and select-object (2021)

    799 shares
    Share 320 Tweet 200
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.