PoshTip #21 – Read a CSV file with PowerShell

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

 

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.