[英]How to find a file with not match string using select-string in PowerShell?
我有很多文件。 我想找到不包含字符串的文件。 我試試這個,但它返回重復的文件名。
$File = Get-ChildItem -Path "D:\*.txt"
$Result = Select-String -Pattern "Identifier -NotMatch $File | Select-Object -ExpandProperty FileName
Write-Host "Result: $Result"
它像這樣返回
1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 158A.txt 158A.txt 158A.txt 158A.txt 158A.txt 158A.txt AB8A.txt AB8A.txt AB8A.txt AB8A.txt AB8A.txt
任何人都可以幫忙。 謝謝
-NotMatch
將使Select-String
返回不匹配的行,它在文件級別不起作用。
做你想做的事,通過Select-String
運行所有文件,然后過濾掉那些有任何匹配的文件:
$result = Get-ChildItem -Path D:\ -Filter *.txt |Where-Object {
@($_ |Select-String -Pattern "Identifier" |Select-Object -First 1).Count -eq 0
} |Select-Object -ExpandProperty FileName
我們從有問題的文件中獲取第一個可能的匹配項 - 如果沒有找到,則數組表達式將為空並且Count -eq 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.