PowerAddict.net

powerAddict.NET

by Lou Goban
IT Application Professional at Syntax

Create distribution list via PowerShell with user list

This short script will create Distribution List/Group, add manager and members from provided text/csv file.

TXT or CSV file have to have the same name as group which will be created.

Example:

Group name: DLno1
TXT/CSV file: DLno1.txt or .csv

Usage: .\New-DL-with-Users.ps1 NameOfDL ManagerOfDL

Script

<#
.SYNOPSIS
  Project:	DLs
  Purpose:	Create new DL and add users from the list
.DESCRIPTION
  
.INPUTS
   Name of DL
   Manager of DL
.OUTPUTS
   None
.NOTES
   Version:      1.0
   Author:       Lubomir Goban
   Date:	 30.03.2020
   Updated:		  
  
.EXAMPLE
  .\Create-DL-ADD-Users.ps1 NameOfDL ManagerOfDL
#>

param(
	[Parameter(Mandatory = $true, Position = 1)]
	$script:Name,
    	[Parameter(Mandatory = $true, Position = 2)]
	$script:Manager
    	#[Parameter(Mandatory = $true, Position = 3)]
	#$script:Domain
	)

###
###  GLOBAL VARIABLES  ###
###
#$script:Domain  = "DOMAIN.COM" # COMMENT THIS OUT IF YOU WILL USE PARAMETER FOR DOMAIN
$script:CSVpath = "$env:USERPROFILE\Desktop\$Name.csv" # HEADER SHOULD BE "USER"



###
###  EXECUTION  ###
###
Write-Host "Importing users.." -ForegroundColor Cyan
$users = Import-Csv -Path $CSVpath

clear-host

Write-Host "DL will be created with the following data:" -ForegroundColor Cyan
Write-Host "Name: $Name" -ForegroundColor Yellow
Write-Host "Email: $Name@$Domain" -ForegroundColor Yellow
Write-Host "Managed by: $Manager" -ForegroundColor Yellow
Write-Host "Members count:" ($users).count -ForegroundColor Yellow

Pause

Write-Host "Creating group.." -ForegroundColor Cyan
New-DistributionGroup -Name $Name -PrimarySmtpAddress "$Name@$Domain" -Alias $Name -ManagedBy $Manager
Write-Host "Group was created.." -ForegroundColor Green

Write-Host "Adding users.." -ForegroundColor Magenta

foreach ($user in $users) {

    Write-Progress -Activity $user.User
    Add-DistributionGroupMember -identity "$name@$Domain" -member $user.User

}

Write-Host "Users were added.." -ForegroundColor Green
Write-Host "Task done!" -ForegroundColor Green

Leave the first comment