Created
January 14, 2020 00:36
-
-
Save technion/9fbde583a48dc3f5d09abdbf517f7ba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Export | |
$allGroups = Get-DistributionGroup | select Name, primarysmtpaddress | |
$exportlist = @() | |
foreach($group in $allGroups) { | |
$obj = New-Object -TypeName psobject | |
$obj | Add-Member -MemberType NoteProperty -Name Name -Value $group.Name | |
$obj | Add-Member -MemberType NoteProperty -Name Email -Value $group.PrimarySMTPAddress | |
$members = Get-DistributionGroupMember $group.Name | select Name | |
$obj | Add-Member -MemberType NoteProperty -Name Members ($members | ConvertTo-Json) | |
$exportList += $obj | |
} | |
$exportlist | ConvertTo-Json | Out-File '.\dlexport.json' | |
# Import | |
Set-StrictMode -Version 2 | |
$distributionGroups = Get-Content '.\dlexport.json' | Out-String | ConvertFrom-Json | |
$count = 0 | |
foreach ($dl in $distributionGroups) { | |
$new = New-DistributionGroup -Name $dl.Name -OrganizationalUnit "OU" ` | |
-PrimarySmtpAddress ($dl.Email).Address -RequireSenderAuthenticationEnabled $false -ErrorAction SilentlyContinue | |
$memberList = $dl.members | ConvertFrom-Json | |
foreach ($member in $memberList) { | |
write-host "Adding User $($member.Name) to $($dl.name)" | |
$aduser = Get-recipient $member.Name -Erroraction SilentlyContinue | |
if ([string]::IsNullOrWhiteSpace($aduser)) { | |
continue | |
} | |
$new | Add-DistributionGroupMember -Member $aduser.Name -ErrorAction SilentlyContinue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment