PoshTip #21 – Read a CSV file with PowerShell
The Import-CSV cmdlet is used to load a CSV file. The input data is transformed into an object. First, you need to import the data :
Import-CSV -path C:\Computers.csv
The first line in the text file gets parsed as a header:
ID HostName IPAddr -- ----------- ------- 1 VDI01 192.168.0.100 2 VDI89 192.168.0.101 3 VDI43 192.168.0.102 4 VDI19 192.168.0.103
The where clause is used to filter the results:
$Computers = Import-CSV -path C:\Computers.csv $Computers | Where {$_.Hostname -eq "VDI19"} | ft -AutoSize ID HostName IPAddr -- ----------- ------- 4 VDI19 192.168.0.103
Thanks for reading! You can follow me on Twitter @PrigentNico