Search code examples
powershellget-childitem

Powershell Get-ChildItem Exclude Default Windows Folders


I want to search for files with .2fa extension on remote computers. I can find the files I want, but it takes a long time to get to the second computer because it scans all windows files.

I tried the -exclude and where arguments but they do not work.

Could you please help me? Thanks.

$ServerList = Import-Csv 'C:\PC.CSV'

$result = foreach ($pc in $ServerList.barkod) {
$exclude = '*ProgramData*','*Program Files*','*Program Files (x86)*','*Windows*'.'*winupdate*'
$sourcepath = 'c$'

Get-ChildItem -Path \\$pc\$sourcepath -Recurse | Where-Object { $_.Name -like "*.2fa" } |
where {$_.name -notin $Exclude}

}

$result

I tried

-Exclude $exclude -where {$_.name -notin $Exclude}


Solution

  • -exclude doesn't work with subdirectories or -filter:

    Get-ChildItem -Path \\$pc\$sourcepath\ -exclude $exclude | 
      get-childitem -recurse -filter *.2fa