Share via


How to Filter CSV data using PowerShell

One of my team mate asked me "How to Filter CSV data using PowerShell"?

I replied back why do you want to do using PowerShell when you have Excel :) . I really stop people making things complicated. But the scenario he explained me was little interesting.

Requirement:
We have a data center rack details like below

Rack1 , Rack2, Rack3,Rack4
Server1,Server2,Server3,VLAN10
Server4,Server5,Server6,VLAN10
Server7,Server8,Server9,VLAN11
Server10,Server11,Server12,VLAN11

We need to automate by filtering the CSV and send to concern team for downtime approval. Ex: Activity on VLAN10 - Associated servers will be down. I am no really convinced with this requirement because doing this is easy in PowerShell but the technical process is not really aligned in their IT. I am not bothered about his technical process.

I don't want to highlight it to them because they do have network experts to manage it.

So how to do this in PowerShell is the base question to be answered.

Solution:



$data = Import-Csv C:\Users\904870\Desktop\Book1.csv
$result =@()
foreach($d in $data)
    {
        $result += $d | Where-Object {$_.Rack4 -like "VLAN10"}
         
    }
    $result | Export-Csv C:\Temp\VLAN10.csv

Enjoy PowerShell :)