Advertisement
spastek

Exchange Online - Add contact to distro list via CSV file

May 18th, 2022
636
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Admin Check
  2. param([switch]$Elevated)
  3. function Test-Admin {
  4.     $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  5.     $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  6. }
  7.  
  8. if ((Test-Admin) -eq $false)  {
  9.     if ($elevated) {
  10.         # tried to elevate, did not work, aborting
  11.     } else {
  12.         Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
  13.     }
  14.     exit
  15. }
  16.  
  17.  
  18. 'Running with full privileges'
  19.  
  20. #Get Admin Credentials
  21. Write-Host "Please use your full email address as your username"
  22. $UserCredential = Get-Credential
  23.  
  24. #Authenticate with Office
  25. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  26.  
  27. #Connect to Exchange
  28. Import-PSSession $Session -DisableNameChecking
  29.  
  30. #Get CSV path to import
  31. Write-host "Please Enter the full file path to the csv file"
  32. $path = Read-Host "Full File Path to CSV"
  33. Write-Host "Path is set to " $path
  34.  
  35. #CSV Format Example
  36. #Members,Identity
  37. #bob@contoso.com,Distribution Group 1
  38. #sally@contoso.com,Distribution Group 2
  39.  
  40. #Set CSV Headers
  41. $member = "Members"
  42. $distname = "Identity"
  43.  
  44. #Do WhatIf  First so I can grab and easy to read log
  45. Import-csv -Path $path | ForEach-Object { Add-DistributionGroupMember -Identity "$($_.$distname)" -Member "$($_.$member)" -Confirm:$false -WhatIf }
  46.  
  47. Import-csv -Path $path | ForEach-Object { Add-DistributionGroupMember -Identity "$($_.$distname)" -Member "$($_.$member)" -Confirm:$false }
Advertisement
RAW Paste Data Copied
Advertisement