SOLVED

Import-CSV data into to set variables in a script

Copper Contributor

Good morning all,

I don't know much about PowerShell but I am trying to create an script that variable data from each row in CSV file and input data to create the mailbox. For example:

CSV:
Username, Servername, ServerCode
Bob,jane, DC101, 101

 

Input variable to the following: 
"Enable-Mailbox -identity "Domianname\User.name" - Database MBDB01-code -PrimarySMTPAddress "User.Name@email.address" -domain controller FQDN_servername"

Where
User.name = CSV.Username
code = CSV.servercode 
FQDN_servername = CSV.servername

Then the result will either out put the Enable-Mailbox line to .txt file or run each Enable-mailbox line

Regards

 

2 Replies
best response confirmed by Hjb118 (Copper Contributor)
Solution

@Hjb118 

Something like this, but the csv file has bob,jane as username and you should use a Firstname, Lastname colum I guess..  Now the variables are like this:

 

Harm_Veenstra_0-1643539443515.png

 

Script example:

foreach ($user in import-csv D:\temp\users.csv) { 
	Enable-Mailbox -identity "Domainname\$($User.username)" -Database "MBDB01-$($user.servercode)" -PrimarySMTPAddress "$($user.username)@email.address" -domaincontroller "$($user).Servername" -whatif
}

 

Did this work for you?
1 best response

Accepted Solutions
best response confirmed by Hjb118 (Copper Contributor)
Solution

@Hjb118 

Something like this, but the csv file has bob,jane as username and you should use a Firstname, Lastname colum I guess..  Now the variables are like this:

 

Harm_Veenstra_0-1643539443515.png

 

Script example:

foreach ($user in import-csv D:\temp\users.csv) { 
	Enable-Mailbox -identity "Domainname\$($User.username)" -Database "MBDB01-$($user.servercode)" -PrimarySMTPAddress "$($user.username)@email.address" -domaincontroller "$($user).Servername" -whatif
}

 

View solution in original post