Run Powershell using a CSV list as input - Fabian Neve

About SharePoint, Javascript, PowerShell, and other technical stuff

Friday, October 23, 2015

Run Powershell using a CSV list as input

Let's assume you have a CSV list with several columns which you want to use as variables. Something like that:

Name,eMail,Account
Hans Muster,hans.muster@gmail.com,hmuster
Frauke Peters,frauke.peters@hotmail.com,fraukepeters
Hugo Schönbächler,teh_hugo@gmail.com,teh_hugo

Notice, the data is separated by commas (,), not semicolons, not tabs. This is relevant!

Then save the csv as eg. C:\temp\users.csv.

Now comes the powershell script:

$csv = Import-Csv C:\temp\users.csv
foreach ($line in $csv) {
$name = $line.Name
$mail = $line.eMail
$account = $line.Account
}

Inside the brackets { } you put your script after the variables you defined in the first lines. Done.

No comments:

Post a Comment