Avatar of ITISEMEA
 asked on

Difference between the scripts


I have very less knowledge of Powershell Script. I want to know what does the commands in given script mean. I want to understand why this script is different from the one i raised a question on last script. Please explain in detail so that i can learn powershell better.

My old question was https://www.experts-exchange.com/questions/28652035/Powershell-Script.html?anchorAnswerId=40738008#a40738008

$UserList = Import-csv "c:\temp\book1.csv" -Delimiter ";"
$OrganizationalUnit = "OU=Contacts,OU=rvp,OU=rvp_bharat,OU=_Company,DC=vaibhav,DC=rajarshi,DC=local"
$DistributionGroup ="testgroup-lkofod"

ForEach ($User in $UserList) {

$Displayname = $user.LastName+", "+$user.FirstName+" "+"("+$User.Country+")"
$Alias = $user.FirstName+$user.LastName

New-MailContact -DisplayName $Displayname -LastName $user.lastname -FirstName $user.firstname -Alias $Alias -Name $Displayname -ExternalEmailAddress $user.ExternalEmailAddress -OrganizationalUnit $OrganizationalUnit | select name, alias, ExternalEmailAddress

Set-Contact -Identity $Alias -Phone $user.Phone -CountryOrRegion $User.Country

Add-DistributiongroupMember -Identity $DistributionGroup -Member $Alias

PowershellExchangeWindows Server 2008

Avatar of undefined
Last Comment
Will Szymkowski

8/22/2022 - Mon
Will Szymkowski

This is the breakdown...
First Section
In the script you have posted lines 2/3 are using Variables for Organizational Unit and Distribution Group. The variable is now used in the script rather than the full OU path or Distribution Group name. This is useful when you have large scripts and you are using and have the same info in multiple places. When you are using a Variable you just need to change it at the top and it applies to all locations where the variable is present.

Second Section
ForEach is being used in both which tells Powershell to complete all commands in the {} and then move to the next one in the list (your list being the CSV file). This is required.

Third Section
in the script you have posted here you have 2 more variables DisplayName and Alias. This is basically providing the format of how the DisplayName and Alias are constructed based on the values you have in your CSV file. In the script I initially provided it only uses the values REQUIRED Name and Email Address to create the New-MailContact. All other parameters are options. I only added the ones that were required.

Forth Section
Provides another cmdlet Set-Mailcontact on a separate line. This entire process will happen for each user within the {}. This command is present due to not being able to modify those properties when using the new-mailcontact creation. The contact needs to be created first then you can run the Set-Mailcontact cmdlet to modify/update the attributes.

Fifth Section
The last section is adding the Users to a Distribution Group. In my first script I used another ForEach command which will do this after all of the contacts have been created rather than doing it while each contact is being created.

That is the complete breakdown of both of the scripts. Overview is that the second script had move Variables and additional info added for properties that needed to be set during the creation process.


ok thanks and i just need a help on first section:---"This is useful when you have large scripts and you are using and have the same info in multiple places." So i want to ask that why should i change if everything is in excel the script you told earlier should automatically pick it from there, then why this part of script is needed.
Will Szymkowski

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.