top of page

PowerShell Search and Combine CSV

A network engineer was tasked with reviewing extensive log files from switches and firewalls, and all had the same format but were all in separate files. They only cared about very specific data in these files (30+) and had to open and review each one on a quarterly basis. I decided to write a PowerShell Script to filter all the data and combine required reading to a single report (CSV).

Typing out PowerShell Commands

<#  
   .Synopsis

       Use PowerShell to quickly combine multiple CSV files & filter for specific text found

    .DESCRIPTION

        Takes input from folder and selects only CSV files.
        Searches each row and selects only rows of data with select text in the row.
        Exports the data to a CSV file that can be separated out with Text to Columns      

      .EXAMPLE        
        Run the script and place the folder path at prompt:       
        C:\Users\Scott\Downloads\Filter_CSV_Data\ 

#>
    Try
   {            
       # CSV files under the selected folder        
        $Path=Read-host "Enter Folder Path" 
       $Dir = get-childitem $Path -ErrorAction Stop 
       $List = $Dir | where { $_.extension -eq ".csv" } -ErrorAction Stop        
       Foreach ($File in $List.name)
       {    
            $MyFile=Get-Content "$Path\$File" -ErrorAction Stop
           #Loop through data to get only Item without the No response 
           Foreach ($Line in $MyFile)
           {  
               $MyString = $Line.ToString() 
               #Conditional statement to filter out records required 
               If (($Mystring -match "Picked"))
               {
                   #Output data to text file for import to Excel  
                   $Line | out-file $Path\FullList.csv -Append -ErrorAction Stop
               }
           }
       }        
   }
   Catch
   {
       Write-Output $_.Exception 
   } 

PowerShell Script Excel CSV Filter
bottom of page