run powershell command using csv as input
Asked Answered
B

3

13

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?

Blazon answered 26/4, 2011 at 1:56 Comment(0)
E
24
$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.

Elseelset answered 26/4, 2011 at 2:5 Comment(0)
L
3
import-csv .\file.csv  -header ("first","second","third") | foreach{New-Mailbox -Name $_.first -WindowsLiveID $_.second -ImportLiveId}
Lianne answered 26/4, 2011 at 2:9 Comment(2)
does -header add a header column?Blazon
-Header doesn't modify the csv file, but it will add a virtual headerLianne
C
-5

This is some of the most useful information I have seen yet - it just made my job so much easier!!!

Combining Netapp commands:

get volumes from a controller, get snapshot schedule for said volumes, and export to a csv:

get-navol | Get-NaSnapshotSchedule | Export-Csv -path d:\something.csv

Import the csv reading in current values and assigning each column a label.

For each object, create a new schedule by RE-USING 4 of the 5 available columns/data fields

import-csv d:\something.csv -header ("label1","label2","label3","label4","label5") | foreach {Set-naSnapshotschedule $.label1 -Weeks $.label2 -Days $.label3 -Hours $.label4 -Whichhours "1,2,3,4,5"}

EXCELLENT STUFF!!!

Please note that the "Labels" should have an underscore - for whatever reason it isn't reflecting in the page so Dollar($)Underscore(_)Dot(.)Label

Competent answered 9/12, 2013 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.