この回答が以前の回答と重複しているように思われる場合は、お詫び申し上げます。これを解決する更新された(POSH 5.0でテストされた)方法を示したかっただけです。以前の回答は 3.0 より前のものであり、最新のソリューションほど効率的ではありませんでした。
ドキュメントはこれについて明確ではありませんが、親パス ( ) ではなく、Get-ChildItem -Recurse -Excludeリーフ ( ) の除外のみに一致します。除外を一致させると、葉が一致するアイテムが削除されるだけです。その葉にまだ再帰します。Split-Path $_.FullName -LeafSplit-Path $_.FullName -ParentGet-ChildItem
POSH 1.0 または 2.0 で
Get-ChildItem -Path $folder -Recurse |
? { $_.PsIsContainer -and $_.FullName -inotmatch 'archive' }
注: @CB と同じ答え。
POSH 3.0+ で
Get-ChildItem -Path $folder -Directory -Recurse |
? { $_.FullName -inotmatch 'archive' }
注: @CB からの回答を更新しました。
複数の除外
これは、パラメーターでリーフを除外し、 (大文字と小文字を区別しない like) 比較Excludeで親を除外しながら、ディレクトリを明確にターゲットにします。ilike
#Requires -Version 3.0
[string[]]$Paths = @('C:\Temp', 'D:\Temp')
[string[]]$Excludes = @('*archive*', '*Archive*', '*ARCHIVE*', '*archival*')
$files = Get-ChildItem $Paths -Directory -Recurse -Exclude $Excludes | %{
$allowed = $true
foreach ($exclude in $Excludes) {
if ((Split-Path $_.FullName -Parent) -ilike $exclude) {
$allowed = $false
break
}
}
if ($allowed) {
$_
}
}
注:$Excludes大文字と小文字を区別する場合は、次の 2 つの手順があります。
Excludeからパラメータを削除しますGet-ChildItem。
- 最初の
if条件を次のように変更します。
if ($_.FullName -clike $exclude) {
注:このコードには、本番環境では決して実装しない冗長性があります。正確なニーズに合わせて、これをかなり単純化する必要があります。これは詳細な例として役立ちます。