Powershell csv filter

Seeking help on best practice since I'm new to Powershell,

I have a csv file and I would like to filter out every line in csv except for lines containing "Not Installed"

Then I would like to filter out these results for individual csv files containing a list of computers, and also exclude any lines containing a match.

Any help or starting points would be appreciated!

+5
source share
3 answers

In the first part you can use Select-String -Notmatch ...for example:

$file = "data.csv"
$csv = Get-Content $file
$csv | Select-String -Notmatch 'Not Installed' | Out-File $file -Enc ascii

CSV Unicode, "-Enc ascii", Out-File Unicode. , . Import-Csv Export-Csv:

man Import-Csv -full
man Export-Csv -full
+5

get-help *csv*

get-help import-csv -examples

csv test.txt

header1,header2,header3
installed,2,3
not installed,2,3

powershell .

$p = Import-CSV .\test.txt
$p | Where { $_.header1 -eq 'installed'}
+3

, , .

, , :

$Filtered = $Table|Select-String -Pattern $ExcludeStrings -NotMatch -SimpleMatch
$FilteredArray = @()
$Filtered.Line | foreach {$split = $_ -split ';';
                          $tidied = $split -replace '@|{|}|^ ';
                          $obj = New-Object System.Object
                          $tidied|foreach {$obj|Add-Member -MemberType NoteProperty -Name ($_.split('=')[0]) -Value ($_.split('=')[1])
                                            }
                          $FilteredArray += $obj  
                            }

, Import-CSV, , .

, MatchInfo, :

@{Column1=Value1; Column2=Value2; Column3=Value3;}

CSV , .

, ($FilteredArray).

Then I passed Linethe matchinfo array property from Select-Stringto the foreach loop, and for each line I split it into half-time, then I delete the @ {} characters and any leading spaces.

Then I create a new one System.Object, then translating the result into another foreach loop, creating new NoteProperty objects in obj, calling them from the current line in finished form to the = character, and the value is the remaining text after the = character.

As a result, I get:

PS C:\Users\Graham> $obj

Column1            : Value1
Column2            : Value2
Column3            : Value3

And the filtered array looks like this:

PS C:\Users\Graham> $FilteredArray[0..3]|Format-table

Column1         Column2        Column2                                    
-------         -------        -------
Value1          Value2         Value3 
Value1          Value2         Value3
Value1          Value2         Value3
0
source

All Articles