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.

  • The -exclude option of Get-ChildItem works only on leaves, not path elements. You'll have to pipe to a where to exclude. – LotPings Apr 7 '17 at 15:42
  • Can you give me an example? – gsb005 Apr 7 '17 at 15:52
  • 1
    See the answer to this question – LotPings Apr 7 '17 at 16:14
  • This was exactly what I was looking for @Lotpings!! thanks!! – gsb005 Apr 7 '17 at 16:39
  • Feel free to vote up that answer ;-) – LotPings Apr 7 '17 at 16:42
0

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
  • Thank you so much Techie007. It is amazing to me that; ---- PowerShell does not have a way to exclude a folder and everything underneath it? seems like such a basic premise. – gsb005 Apr 7 '17 at 15:54
  • Unfortunately, $mysys = 'c:\windows*' didn't work, it's still looking underneath it. – gsb005 Apr 7 '17 at 15:58
  • "Unfortunately, $mysys = 'c:\windows\*' didn't work" Just doing that isn't going to fix anything, it's just better/proper syntax for "everything starting with 'c:\windows\`". :) – Ƭᴇcʜιᴇ007 Apr 7 '17 at 15:59
  • Techie007, can you please shed some light here, can you please tell me if somehow using, $env:SystemRoot, parameter in the script above will bear positive results? – gsb005 Apr 7 '17 at 16:03
  • Using the $env:SystemRoot environment variable is better to use if you'll be reusing the script (on different computers), as Windows can be installed to a location other than c:\windows, hence why that system variable exists. :) Having said that, using that variable instead of c:\windows in the code in question will make no difference. – Ƭᴇcʜιᴇ007 Apr 7 '17 at 16:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.