Search code examples
emailpowershelloutlookdistribution-listcontactgroups

Add an email to outlook distribution list using powershell


I just create a new distribution list on Outlook byt he following script

$outlook = new-object -com Outlook.Application
$contacts = $outlook.Session.GetDefaultFolder(10)
$dl = $contacts.Items.Add("IPM.DistLIst")
$dl.DLName = "Group A"
$dl.Save()

and I Have an e-mail address "[email protected]" with name to be "manager"

how do i use powershell to add this to the newly created distribution list?

I have to use powershell due to some reason, and I have tried this:

Add-DistributionGroupMember -Idneity "Group A" -Member "[email protected]"

But gives this error:

The term 'Add-DistributionGroupMember' is not recognized as the name of a cmdlet, function, 
script file, or operable program.

Please help

[UPDATE] Now I have a script that works:

  $outlook = new-object -com Outlook.Application
  $contacts = $outlook.Session.GetDefaultFolder(10)
  $session = $outlook.Session
  $session.Logon("Outlook")
  $namespace = $outlook.GetNamespace("MAPI")
  $recipient = $namespace.CreateRecipient("John [email protected]")  # this has to be an exsiting contact
  $recipient.Resolve()  # check if this returns true
  $DL = $contacts.Items.Add("IPM.DistList")
  $DL.DLName = "test dl"
  $DL.AddMember($recipient)
  $DL.Save()

Solution

  • AddMember only allows to pass a Recipient object as a parameter: call Application.Session.CreateRecipient("[email protected]") / Recipient.Resolve / DistListItem.AddMember(Recipient).

    If you need to add a contact directly, you can use Redemption (I am its author) and its RDODistListItem.AddContact method.

    UPDATE: In Redemption, the following code adds a one-off member to a new DL list:

      set Session = CreateObject("Redemption.RDOSession")
      Session.MAPIOBJECT = Application.Session.MAPIOBJECT
      set Contacts = Session.GetDefaultFolder(olFolderContacts)
      set DL = Contacts.Items.Add("IPM.DistList")
      DL.DLName = "test dl"
      DL.AddMember("[email protected]")
      DL.Save