Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run powershell command using csv as input

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?

like image 669
Hailwood Avatar asked Apr 26 '11 01:04

Hailwood


2 Answers

$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.

like image 179
manojlds Avatar answered Oct 06 '22 22:10

manojlds


import-csv .\file.csv  -header ("first","second","third") | foreach{New-Mailbox -Name $_.first -WindowsLiveID $_.second -ImportLiveId}
like image 20
LanceHub Avatar answered Oct 06 '22 22:10

LanceHub