SOLVED

Import-CSV data into to set variables in a script

%3CLINGO-SUB%20id%3D%22lingo-sub-3087225%22%20slang%3D%22en-US%22%3EImport-CSV%20data%20into%20to%20set%20variables%20in%20a%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3087225%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20morning%20all%2C%3CBR%20%2F%3E%3CBR%20%2F%3EI%20don't%20know%20much%20about%20PowerShell%20but%20I%20am%20trying%20to%20create%20an%20script%20that%20variable%20data%20from%20each%20row%20in%20CSV%20file%20and%20input%20data%20to%20create%20the%20mailbox.%20For%20example%3A%3CBR%20%2F%3E%3CBR%20%2F%3ECSV%3A%3CBR%20%2F%3EUsername%2C%20Servername%2C%20ServerCode%3CBR%20%2F%3EBob%2Cjane%2C%20DC101%2C%20101%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EInput%20variable%20to%20the%20following%3A%26nbsp%3B%3CBR%20%2F%3E%22Enable-Mailbox%20-identity%20%22Domianname%5CUser.name%22%20-%20Database%20MBDB01-code%20-PrimarySMTPAddress%20%22User.Name%40email.address%22%20-domain%20controller%20FQDN_servername%22%3CBR%20%2F%3E%3CBR%20%2F%3EWhere%3CBR%20%2F%3EUser.name%20%3D%20CSV.Username%3CBR%20%2F%3Ecode%20%3D%20CSV.servercode%26nbsp%3B%3CBR%20%2F%3EFQDN_servername%20%3D%20CSV.servername%3CBR%20%2F%3E%3CBR%20%2F%3EThen%20the%20result%20will%20either%20out%20put%20the%20Enable-Mailbox%20line%20to%20.txt%20file%20or%20run%20each%20Enable-mailbox%20line%3CBR%20%2F%3E%3CBR%20%2F%3ERegards%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3087225%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecommunity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3088560%22%20slang%3D%22en-US%22%3ERe%3A%20Import-CSV%20data%20into%20to%20set%20variables%20in%20a%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3088560%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1291087%22%20target%3D%22_blank%22%3E%40Hjb118%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomething%20like%20this%2C%20but%20the%20csv%20file%20has%20bob%2Cjane%20as%20username%20and%20you%20should%20use%20a%20Firstname%2C%20Lastname%20colum%20I%20guess..%26nbsp%3B%20Now%20the%20variables%20are%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Harm_Veenstra_0-1643539443515.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F343639iB35A4D4997D6D32A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Harm_Veenstra_0-1643539443515.png%22%20alt%3D%22Harm_Veenstra_0-1643539443515.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20example%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach%20(%24user%20in%20import-csv%20D%3A%5Ctemp%5Cusers.csv)%20%7B%20%0A%20Enable-Mailbox%20-identity%20%22Domainname%5C%24(%24User.username)%22%20-Database%20%22MBDB01-%24(%24user.servercode)%22%20-PrimarySMTPAddress%20%22%24(%24user.username)%40email.address%22%20-domaincontroller%20%22%24(%24user).Servername%22%20-whatif%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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 (Occasional 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?