A script I recently used to add members to an office 365 distribution group using a remote exchange session in a hybrid setup (covered at the end).
1 2 3 4 5 |
[cc lang="powershell"]$SourceFile = "" Import-CSV $SourceFile | ForEach ` {Add-DistributionGroupMember -Identity "Group Name" -Member $_.UPN Write-Host -ForegroundColor Green "Processed the record for $($_.UPN)" }[/cc] |
CSV file contains UPNs of all users to be added.
I also found it useful to see who had permissions to send to this group using
1 |
[cc lang="powershell"](Get-DistributionGroup -Identity "Group Name").AcceptMessagesOnlyFrom | ForEach {Get-User -Identity $_}[/cc] |
To open remote exchange shell:
1 |
[cc lang="powershell"]$UserCredential = Get-Credential[/cc] |
A prompt will appear for your login credentials then,
1 2 |
[cc lang="powershell"]$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session[/cc] |
Afterwards, remember to remove the session
1 |
[cc lang="powershell"]Remove-PSSession $Session[/cc] |