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
  • 2
  • 6

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
Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
  • 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 07 '17 at 15:54
  • Unfortunately, $mysys = 'c:\windows\*' didn't work, it's still looking underneath it. – gsb005 Apr 07 '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 07 '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 07 '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 07 '17 at 16:06
  • Techie007, to you is it odd that PowerShell, as you have explained it, has *no simple way* of excluding an entire folder and its chiild items? – gsb005 Apr 07 '17 at 16:11
  • No it's not odd to me, as GCI is returning an array of objects, not a simple text list. What's odd to me is that you find getting Access Denied errors during this to be a problem. :) – Ƭᴇcʜιᴇ007 Apr 07 '17 at 16:17
  • Note: the solution(s) on the question @LotPings provided in his comment still implement the ContinueSilently Error Action (for the same reasons mentioned here. :) ) – Ƭᴇcʜιᴇ007 Apr 07 '17 at 16:22
  • access denied errors are fine, and i can live with those, like you explained with even a 'SilientlyContinue' switch. – gsb005 Apr 07 '17 at 16:22
  • the main problem is; i don't want my "search" to go into the realm of the windows folder, i don't want anything from that folder; hence i want to avoid it like the plague, if there is a dll that i want in the future, only then i'll point my energies at that folder. I don't want my recursive search to span c:\windows and anything underneath it. For one thing, later on, I'll want to pipe a "move" command to move some of those txt files into another directory and want to avoid "changing" or risk "changing" in the systemroot folder. – gsb005 Apr 07 '17 at 16:24
  • So the act of searching in the Windows folder isn't actually the concern, it's that you don't want results from within the Windows folder, even if you're allowed into the folder, and it files that match you criteria -- correct? – Ƭᴇcʜιᴇ007 Apr 07 '17 at 16:30
  • Exactly, Techie007. BTW, thanks for all the tips here so far. – gsb005 Apr 07 '17 at 16:33
  • Then I would look at using a Where-Object to filter the results as suggested in the Q&A linked to by @LotPings: https://superuser.com/questions/1190355/powershell-get-childitem-include-exclude-simple-script-not-working-properly :) – Ƭᴇcʜιᴇ007 Apr 07 '17 at 16:35
  • Excellent!! Thanks for sticking around and answering all the questions, have a good day sir. – gsb005 Apr 07 '17 at 16:36