I am trying to exclude some directories when using get-childitem
I know this way will
Get-ChildItem c:\users |where {$.name -NotLike “*SUP” -and $.name -notlike “Cit*”}
Get-ChildItem c:\users |where {$_.name -Notmatch “SUP|Cit|pcg”}
I am trying to make it looks more nicer and easy to change in future
$e=@(‘_sup’,‘sa’,‘pcg’,'srv,‘citrix’,‘administrator’)
Get-ChildItem c:\users |where {$_.name -NotLike $e }
OR
$e=“_sup|sa|citrix”
Get-childitem c:\users \where {$_.name -notmatch $e
It doesn’t exclude any directory. can someone help?
The following work for me:
$exclusion = 'Sales|Corp'
Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch $exclusion}
or
$exclusion = 'Sales','Corp'
Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch ($exclusion -join '|')}
The directories are:
PS C:\WINDOWS\system32> Get-ChildItem C:\Scripts -Directory
Directory: C:\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/6/2019 11:16 AM Corp
d----- 11/6/2019 11:16 AM Legal
d----- 12/6/2019 9:04 AM Sales
Both methods filter with the result:
PS C:\WINDOWS\system32>
$exclusion = 'Sales','Corp'
Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch ($exclusion -join '|')}
Directory: C:\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/6/2019 11:16 AM Legal
js2010
3
Wouldn’t it be fullname not name?
Get-ChildItem \\path\test -Exclude FolderT,FileW
# Exclude folder and file in array
$list = @('FolderT','FileW')
Get-ChildItem \\path\test -Exclude $list