Search code examples
powershellcsvtrim

Trim multiple items CSV using PowerShell


Have some base code I need modified to support the filtering of multiple items in the results field based on different strings. For example I need to filter out everything that doesn't follow a pattern of "Version is 2.x.x.x.x" or " found" or any number of other combinations.

$FileIn  = '.\FileIn.Csv'
$FileOut = '.\FileOut.Csv'
$Keywords = '\sVersion is*', '\sFound\s'
Foreach($keyword in $keywords){
(Get-Content $FileIn) -replace $keyword,'' | Set-Content $FileOut }

Solution

  • Got it.....I'm sure there is a cleaner version of my regular expressions but it works.

    $Keywords = '\sVersion\sis\s.{2,16}', '\sfound', '\sshould.{2,40}','\sfile'
    $Csv | ForEach-Object {
    ForEach($keyword in $Keywords){
    $_.Results = $_.Results -replace $keyword,''
     }}
    $Csv | Export-Csv $FileOut -NoTypeInformation