Ask Your Question
2

How can a PowerShell script be created to execute two distinct commands on information sourced from a CSV file that consists of two columns?

asked 2023-07-19 13:44:38 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-19 13:46:02 +0000

nofretete gravatar image

Assuming the CSV file contains two columns named "Column1" and "Column2", and the two distinct commands to be executed are "Command1" and "Command2", the following PowerShell script can be created:

$data = Import-Csv "path\to\file.csv"
foreach ($row in $data) {
    $value1 = $row.Column1
    $value2 = $row.Column2
    Command1 $value1
    Command2 $value2
}

Explanation:

  1. The Import-Csv cmdlet is used to read the CSV file and store the data in the $data variable.
  2. The foreach loop is used to iterate through each row in the $data variable.
  3. The two values in each row are stored in separate variables ($value1 and $value2).
  4. The Command1 and Command2 are executed with the respective values from each row.

Note: Replace "Command1" and "Command2" with the actual commands to be executed, and replace "path\to\file.csv" with the actual path to the CSV file.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-19 13:44:38 +0000

Seen: 14 times

Last updated: Jul 19 '23