Import-CSV and Foreach Import-CSV and Foreach powershell powershell

Import-CSV and Foreach


You can create the headers on the fly (no need to specify delimiter when the delimiter is a comma):

Import-CSV $filepath -Header IP1,IP2,IP3,IP4 | Foreach-Object{   Write-Host $_.IP1   Write-Host $_.IP2   ...}


$IP_Array = (Get-Content test2.csv)[0].split(",")foreach ( $IP in $IP_Array){    $IP}

Get-content Filename returns an array of strings for each line.

On the first string only, I split it based on ",". Dumping it into $IP_Array.

$IP_Array = (Get-Content test2.csv)[0].split(",")foreach ( $IP in $IP_Array){  if ($IP -eq "2.2.2.2") {    Write-Host "Found $IP"  }}


Solution is to change Delimiter.

Content of the csv file -> Note .. Also space and , in value

Values are 6 Dutch word aap,noot,mies,Piet, Gijs, Jan

Col1;Col2;Col3a,ap;noo,t;mi esP,iet;G ,ijs;Ja ,n$csv = Import-Csv C:\TejaCopy.csv -Delimiter ';' 

Answer:

Write-Host $csv@{Col1=a,ap; Col2=noo,t; Col3=mi es} @{Col1=P,iet; Col2=G ,ijs; Col3=Ja ,n}

It is possible to read a CSV file and use other Delimiter to separate each column.

It worked for my script :-)