0

Just a simple question regarding how import-csv command is read. if I have something like this in ps (sorry first time poster, and I can't seem to indent block the script below):

$ReadthisCSV = "C:\...\List.csv"

Function Meh{
  param ($readhere)
  import-csv $readhere | foreach-object{
    #do something here
  }
}

Meh $ReadthisCSV

Is it possible for import-csv command to read from an object variable? or can it only read from a direct path. I did a write-host inside function Meh to see if $readhere was passed through correctly, and it did.

Ragnarokkr
  • 2,328
  • 2
  • 21
  • 31

1 Answers1

1

You shouldn't have any issues. The variable is just a reference to your actual data, so while you see: import-csv $readhere

You defined $readhere as $ReadthisCSV, so it is interpreted as: import-csv "C:...\List.csv"

Powershell is nice in its handling of this stuff and nothing is stopping you from just trying it and seeing what happens on your local machine or test machine.

try this:

$ReadthisCSV = "C:...\List.csv"

Function Meh{ param ($readhere) import-csv $readhere }

Meh $ReadthisCSV

You should see your csv contents displayed to the screen with column headers (first row in the csv)

HypnoticPancake
  • 136
  • 2
  • 3