排除Get-Childitem中带有星号的参数



我想使用Get-ChildItem Powershell方法并传递一个列表到-Exclude参数,但我想使用通配符(如带有单个字符串的*$exclude*),以便只包含任何排除项的所有文件将被排除。我该怎么做呢?

我想从Get-ChildItem方法中排除返回所有文件名中只包含排除项的文件。

我使用了带有!(不)。你也可以使用正则表达式。我还使用ALL来获得一个以上的匹配字符串。我将列表设置为大写字母,然后使用ToUpper()来匹配小写字母和大写字母。参见下面的代码

$excludeList = @("LOG", "CSV")
Get-ChildItem -Path "c:temp" `
| where {![Linq.Enumerable]::Any([string[]]$excludeList,  [Func[string,bool]]{ param($excludeItem); return $_.Name.ToUpper().Contains($excludeItem) }) -eq $True} `
| ForEach-Object {Write-Host $_.Name} 

使用Regex

$excludeList = @("LOG", "CSV")
Get-ChildItem -Path "c:temp" `
| where {![Linq.Enumerable]::Any([string[]]$excludeList,  [Func[string,bool]]{ param($excludeItem); return $_.Name.ToUpper() -match $excludeItem }) -eq $True} `
| ForEach-Object {Write-Host $_.Name}

最新更新