Posted On March 29, 2019

PowerShell Script to Copy Members of One Group to Another

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell Script to Copy Members of One Group to Another
#Author: Kim Doan
#Please tell me this code suck so that I can improve # Adding Prerequisite Active Directory Module
if ( !(get-module -name "ActiveDirectory") ) {"ActiveDirectory module not in shell environment. Now importing..."; Add-WindowsFeature RSAT-AD-PowerShell; import-module -name "ActiveDirectory" -DisableNameChecking | out-null } Do{ $fromGroup = Read-Host -Prompt 'Copy From Group:' $toGroup = Read-Host -Prompt 'Copy To Group:' # This convoluted code is to be backward compatible with older PowerShell versions $oldMembers=(Get-ADGroupMember -Identity $fromGroup -Recursive); $newMembers=(Get-ADGroupMember -Identity $toGroup -Recursive); $old=@(); $new=@(); $index=0; foreach ($newMember in $newMembers){$new+=,$newMember.name} foreach ($oldMember in $oldMembers){$old+=,$oldMember.name} foreach ($o in $old){ if ($new -contains $o){#Do nothing#"$o already exists at the destination";} else { Add-ADGroupMember -Identity $toGroup -Members $oldMembers[$index]; "New entry '$o' from '$fromGroup' has been copied to the group '$toGroup'"; } $index+=1; } $flag = Read-Host -Prompt 'Press Any Key = exit; C = Continue Copying...'} while ($flag -match '[Cc]')

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Use WinSCP to Test a SFTP Connection

# testSftpConnect.ps1 # Quick script to test SFTP connectivity $username='FTPUSER' $password='PASSWORD' $sftpServer='x.x.x.x' $sftpPort=22 function testSftpConnect($sftpServer,$username,$password,$sftpPort){…

Apache Multiple Domains Config

vim /ect/httpd/conf/httpd.conf <VirtualHost *:80>      ServerAdmin [email protected]      ServerName kimconnect.com      ServerAlias www.kimconnect.com      DocumentRoot…

PowerShell: Restore SQL Database from Full and/or Differential Backups

$databaseName='BALOO_MSCRM' $newDatabaseName='BALOO_MSCRM' $dbData='mscrm' # set this value to $null for autogen defaults $dbLog='mscrm_log' # set…