PowerShell – Exclude text on Select-String
PowerShell – Exclude text on Select-String
From the reference of Select-String
, parameter -Exclude
:
Exclude the specified items. The value of this parameter qualifies the
Path parameter. Enter a path element or pattern, such as *.txt.
Wildcards are permitted.
So this is not what we need here.
As noted in the comments, you should filter $AVList
instead:
# Get all AVs, excluding Windows Defender
$filteredAvList = @($AVList.displayName) -notlike *Windows Defender*
if (Select-String -Path $workingdirectoryAVList.txt -Pattern $filteredAvList -SimpleMatch -Quiet){
# TODO: Replace $avList by $filteredAvList
}
When a comparison operator like -notlike
is applied to a collection, it effectively filters the collection, returning only the elements that match the condition. To make sure that the operator is always applied to an array, enclose the LHS operand with the array sub-expression operator @()
. Otherwise you would get a boolean result when the LHS is a single object.
PowerShell – Exclude text on Select-String
Related post:
- Python equivalent of Java StringBuffer?
- replace special characters in a string python.
- What is the difference between the meaning of 0x and x in Python hex strings?
- Convert string to variable name in python.
- Python Json loads() returning string instead of dictionary?
- python – How to check a string for specific characters?
- python – Convert string to integer using map().
- regex – Converting a string variable to a regular expression in python.
- Python SQL query string formatting.
- Converting a single line string to integer array in Python.
- xml – Convert Python ElementTree to string.
- Python – Split string with space delimiter.
- Convert string of 0 and 1 to its binary equivalent python.
- string – Python: how to print range a-z?
- How to sort the letters in a string alphabetically in Python.