1

I want a simple way to exclude the system folder from the searches in PowerShell.

I use the following script:

$mysys = 'c:\windows'    
PS C:\> Get-ChildItem -path . -Include *.txt -Exclude '$mysys' -Recurse

I basically want anything under c:\windows to be completely ignored, however, when I run the search, i continuously get errors as it searches under c:\windows; and in some cases also gives out results that it found.

Error: (why is it even going under c:\windows when I excluded it)

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' is denied. At line:1

char:1 + Get-ChildItem -path . -Include *.txt -Exclude '$mysys' -Recurse -Dept ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessExcept ion + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

For example, it includes: (this should all be excluded) (basically anything under c:\windows)

> >     Directory: C:\Windows\WinSxS\amd64_microsoft-windows-c..iser-inboxdatafiles_31bf3856ad364e35_10.0.14393.0_none_9eeac2cef7a25999
> 
> 
> Mode                LastWriteTime         Length Name                 
> 
> ----                -------------         ------ ----                                                                                
> -a----        7/16/2016   6:42 AM         855814 hwcompat_RS1.txt                                                                    
> ------        7/16/2016   6:42 AM           1764 hwexclude_RS1.txt                                                                   
> ------        7/16/2016   6:42 AM           1327 wucompat.txt

I basically want to do a recursive search under c:\ and then exclude whatever I don't want. Starting with c:\windows and everything under it.

gsb005
  • 31

1 Answers1

1

Suggest a couple minor syntax corrections (these changes won't solve your problem, it's just better syntax):

$mysys = 'c:\windows\*'    
Get-ChildItem -path . -Include *.txt -Exclude $mysys -Recurse

Anyhow...

why is it even going under c:\windows when I excluded it

You're only excluding things in "C:\windows" from the results of Get-ChildItem. GCI still needs to check in the folders you told it to check, to see if anything matches, which causes the Access Denied error.

If you want to completely avoid folders you don't have access to, then you'll need to build a list of folders you wish to check with GCI, instead of just telling GCI to check everything in the current path.

The only way to do this is basically the same procedure: check every folder, and keep a list of the ones that you don't get Access Denied on, then use that list with GCI. In the end you're still attempting to access the folders (and throwing the error) anyway, so there's basically no gain to doing that (expect additional complexity).

If you just don't like seeing the red errors while running the command, you can use the -ErrorAction parameter and tell GCI to continue silently on error. For example:

Get-ChildItem -path . -Include *.txt -Exclude $mysys -Recurse -ErrorAction SilentlyContinue