# Specify the name of the distribution group and security group
$DistributionGroupName = "Test DL1"
$SecurityGroupName = "TestSecurityGroup"
# Get members of the distribution group
$DistributionGroupMembers = Get-DistributionGroupMember -Identity $DistributionGroupName
# Add members to the security group
foreach ($member in $DistributionGroupMembers) {
Add-DistributionGroupMember -Identity $SecurityGroupName -Member $member
# Retrieve the DisplayName and PrimarySmtpAddress of the user
$user = Get-Recipient -Identity $member
Write-Host "Added $($user.DisplayName) ($($user.PrimarySmtpAddress)) to $SecurityGroupName"
}
Write-Host "Members copied from $DistributionGroupName to $SecurityGroupName successfully."
#设置某个用户成为所有通讯组的所有者
# Specify the user to set as the owner
$ownerUser = "Bob Ross"
# Get all distribution groups
$distributionGroups = Get-DistributionGroup -ResultSize Unlimited
foreach ($group in $distributionGroups) {
# Set the owner for each distribution group
Set-DistributionGroup -Identity $group.Identity -ManagedBy $ownerUser -BypassSecurityGroupManagerCheck
Write-Host "Set $ownerUser as the owner of $($group.DisplayName)"
}
##从所有通讯组中删除特定成员
# Specify the member you want to remove from distribution groups
$memberToRemove = "bobs@office365concepts.onmicrosoft.com"
# Get all distribution groups
$distributionGroups = Get-DistributionGroup -ResultSize Unlimited
foreach ($group in $distributionGroups) {
# Check if the member is a member of the group
if (Get-DistributionGroupMember -Identity $group.Identity | Where-Object {$_.PrimarySmtpAddress -eq $memberToRemove}) {
# Remove the member from the distribution group
Remove-DistributionGroupMember -Identity $group.Identity -Member $memberToRemove -Confirm:$false
Write-Host "Removed $memberToRemove from $($group.DisplayName)"
}
}
#使用csv 从所有通讯组中删除用户
# Read the CSV file
$groups = Import-Csv -Path "C:\CSV Files\RemoveMembers.csv"
foreach ($group in $groups) {
# Get members of the distribution group
$members = Get-DistributionGroupMember -Identity $group.Identity
# Remove each member from the distribution group
foreach ($member in $members) {
Remove-DistributionGroupMember -Identity $group.Identity -Member $member.PrimarySmtpAddress -Confirm:$false
Write-Host "Removed $($member.PrimarySmtpAddress) from $($group.DisplayName)"
}
}
#替换通讯组owner
# Specify the current owner and the new owner
$currentOwner = "currentowner@example.com"
$newOwner = "newowner@example.com"
# Get all distribution groups owned by the current owner
$groupsOwnedByCurrentOwner = Get-DistributionGroup -ResultSize Unlimited | Where-Object {$_.ManagedBy -eq $currentOwner}
foreach ($group in $groupsOwnedByCurrentOwner) {
# Set the new owner for the distribution group
Set-DistributionGroup -Identity $group.Identity -ManagedBy $newOwner -BypassSecurityGroupManagerCheck
Write-Host "Changed owner of $($group.DisplayName) from $currentOwner to $newOwner"
}
or
# Specify the current owner and the new owner
$currentOwner = "Bob Ross"
$newOwner = "User1HR"
# Get all distribution groups owned by the current owner
$groupsOwnedByCurrentOwner = Get-DistributionGroup -ResultSize Unlimited | Where-Object {$_.ManagedBy -eq $currentOwner}
foreach ($group in $groupsOwnedByCurrentOwner) {
# Set the new owner for the distribution group
Set-DistributionGroup -Identity $group.Identity -ManagedBy $newOwner -BypassSecurityGroupManagerCheck
Write-Host "Changed owner of $($group.DisplayName) from $currentOwner to $newOwner"
}
#查看过去某个时刻创建的通讯组
# Calculate the date 3 days ago
$startDate = (Get-Date).AddDays(-3)
# Get distribution groups created in the last 3 days
$groups = Get-DistributionGroup -ResultSize Unlimited | Where-Object {$_.WhenCreated -ge $startDate}
# Output the distribution groups created in the last 3 days
if ($groups -ne $null) {
Write-Host "Distribution groups created in the last 3 days:"
$groups | Select-Object DisplayName, PrimarySmtpAddress, WhenCreated
} else {
Write-Host "No distribution groups created in the last 3 days."
}
# Calculate the date 100 hours ago
$startDate = (Get-Date).AddHours(-100)
# Get distribution groups created in the last 100 hours
$groups = Get-DistributionGroup -ResultSize Unlimited | Where-Object {$_.WhenCreated -ge $startDate}
# Output the distribution groups created in the last 100 hours
if ($groups -ne $null) {
Write-Host "Distribution groups created in the last 100 hours:"
$groups | Select-Object DisplayName, PrimarySmtpAddress, WhenCreated
} else {
Write-Host "No distribution groups created in the last 100 hours."
}
# Calculate the date 4 weeks ago
$startDate = (Get-Date).AddDays(-28)
# Get distribution groups created in the last 4 weeks
$groups = Get-DistributionGroup -ResultSize Unlimited | Where-Object {$_.WhenCreated -ge $startDate}
# Output the distribution groups created in the last 4 weeks
if ($groups -ne $null) {
Write-Host "Distribution groups created in the last 4 weeks:"
$groups | Select-Object DisplayName, PrimarySmtpAddress, WhenCreated
} else {
Write-Host "No distribution groups created in the last 4 weeks."
}