run powershell command using csv as input

Hailwood picture Hailwood · Apr 26, 2011 · Viewed 87.8k times · Source

I have a csv that looks like

Name, email, address
Name, email, address
Name, email, address

I am wanting to run New-Mailbox -Name "*Name*" -WindowsLiveID *email* -ImportLiveId

(where *x* is replaced by the value from the csv).

on each line in the csv file.

How can I do this?

Answer

manojlds picture manojlds · Apr 26, 2011
$csv = Import-Csv c:\path\to\your.csv
foreach ($line in $csv) {
    New-Mailbox -Name $line.Name -WindowsLiveID $line.Email -ImportLiveId
}

First line of csv has to be something like Name,Email,Address

If you cannot have the header in the CSV, you can also have:

$csv = Import-Csv c:\path\to\your.csv -Header @("Name","Email","Address")

-Header doesn't modify the csv file in any way.