2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell Search and Combine CSV

<#

     ScritpsByScott.com
     
    .Synopsis

       To quickly combine multiple CSV files and select only rows with 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:
        Example:
        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 
    }