Dries Van den Bergh
Dries Van den Bergh

Reputation: 81

Get filtered content from text file with Get-Content

I'm a bit stuck here. Basically I have a script that reads directory and filepath from a text file.

I want to filter them by driveletter and exclude paths like program files and the C:\Windows Directory. There is a variable for each driveletter But when I use the piece of code below it does not seem to filter anything. It does not seem to matter if the Contains has C E or G in it. it wil always put the whole content of the SaveMe.txt file in every variable.

Any advice?

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("C:\")) -and $_ -NotContains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" }
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("E:\")) -and $_ -NotContains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" }
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("G:\")) -and $_ -notcontains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" }

Upvotes: 1

Views: 1625

Answers (4)

Esperento57
Esperento57

Reputation: 17462

try like this

Solution 1:

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("C:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") }
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("E:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") }
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("G:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") }

Solution 2:

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("C:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" }
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("E:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" }
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("G:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" }

Upvotes: 1

Esperento57
Esperento57

Reputation: 17462

optimized solution (file open only 1x)

$CDrive=@()
$EDrive=@()
$GDrive=@()

Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_ -notlike "*Program Files*" -and $_ -notlike "*C:\Windows*" } | %{ if ($_.Contains("C:\")) {$CDrive+=$_};if ($_.Contains("E:\")) {$EDrive+=$_};if ($_.Contains("G:\")) {$GDrive+=$_};}

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58931

As mentioned, you should use the builtin compare function -like and -notlike. Also you can't combine the -or statements without specifiying the compare function again.

However, I think would make this more readable:

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt |  where { $_ -match 'C:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'}
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt |  where { $_ -match 'E:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'}
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt |  where { $_ -match 'G:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'}

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72151

You need to use -like and -notlike. Contains is for checking if something is in the array, not a string

Upvotes: 0

Related Questions