Creating a dynamic distribution list using an imported CSV file

I have a CSV file that has a repeated job code number and various office location numbers. I am wanting to create a dynamic distribution list with this information. I have included a screenshot below.


I would like there to be parameters that would ask questions such as ListName, Alias and then have it use the imported CSV file as the RecipientFilter criteria. Is this able to be done?

Yes, all that is doable. Did you try anything or are you expecting any ready made script ?
If you have given it a try, post the script with the error/difficulty you are facing, else please give it a try.

Thanks for the reply kvprasoon.

Yes, I tried the following code:



Param (
[Parameter(Mandatory=$True,Position=0)][string]$ConfigFile = $(throw '- Need parameter input file (e.g. "c:\users.csv")'),
[Parameter(Mandatory=$True,HelpMessage='Enter the Name of DG')][string]$DGName,
[Parameter(Mandatory=$True,HelpMessage='Enter the Alias')][string]$aliasDG,
[Parameter(Mandatory=$True,HelpMessage='Enter the JobCode')][string]$jobcode
$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size (500, 300)
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir  = Split-Path -Parent $ScriptPath
$Computer = $env:computername
$TimeStamp = (Get-Date).ToString('_yyyyMMdd-HHmmss')
start-transcript -path $ScriptDir\GoodSam.CreateWebContactGroups_$TimeStamp.txt
$csvinput = import-csv $ConfigFile 
####End Common Setup ####

foreach ($line in $csvinput)
$group=New-DynamicDistributionGroup -Name $DGName -Alias $aliasDG -RecipientFilter {((RecipientType -eq 'CenterNumber') -and (CustomAttribute1 -eq '$jobcode')) }
set-dynamicdistributiongroup -identity $group -HiddenFromAddressListsEnabled $true -RequireSenderAuthenticationEnabled $false


The code fails and does not generate a Dynamic Group.

Well, here are some comments.

  • Parameter $ConfgFile is declared as mandatory, but you have a default value of a throw statement(hope you kept it for custom error message). Since the Parameter is mandatory, PowerShell is not gonna consider the default value to throw the custom error if no value is provided, so you can make mandatory to $false
<li>You have CenterNumber picked from CSV and assigned to $center variable but, 'CenterNumber' is what you have used for filtering.</li>

<li>$JobCode variable is inside single quotes, hence it will get treates as pure string than a variable, quotes are not required, see more at <a href="" rel="noopener" target="_blank">about_quoting_rules</a></li>