0

Having a weird problem with Get-ChildItem. I'm trying to search the C: drive for office files but exclude some folders within it. I tried so many ways for this. I first tried excluding directly with Copy-item but that didn't work. Then I tried Get-ChildItem but that stops at certain folders saying access denied.

I tried this method in this link. If i do only one folder, it skips it like it is supposed to. But some other folders then get searched even tho that folder is in the exclude list.

So here is my code.

$source = "C:\"
$destination = "C:\Backup"
$Exclude =  @('*C:\PerfLogs*' , "Backup" , "boot" , "MSOCache" , "programdata" , "Recovery" ,        "TrueDelete" , "Users", "Windows", "Documents and Settings", "Program Files (x86)\Google\" , "System   Volume Information" , "`$Recycle.Bin" , "Config.Msi" , "bmm")
$includes ="*.xls " , "*.xlsx" , "*.doc" , "*.docx" , "*.ppt" , "*.pptx"
$items = Get-ChildItem $source -Recurse -Exclude $exclude -Include $includes | ?{ $_.fullname -notmatch "\\perflogs\\?" -or "\\backup\\?" } | Copy-Item -Destination $Destination -Container -Force

I initially wanted to copy everything with the folder tree but that doesn't work either. After running this code it first stops at "Perflogs" and if I let it continue it just searches and gets access denied on some other folders that are in the exclude list.

Get-ChildItem : Access to the path 'C:\PerfLogs\' is denied. At C:\pstools\backup\2.ps1:6 char:10 + $items = Get-ChildItem $source -Recurse -Exclude $exclude -Include $includes | ? ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Cheers!

Community
  • 1
  • 1
Besiktas
  • 331
  • 1
  • 6
  • 23
  • Are you running this from an elevated prompt? Also the `Where-object` clause is most likely not working as you wanted. `?{ $_.fullname -notmatch "\\(perflogs|backup)\\?"}` would be how i would do it... although it mostly is not required if you fix the first part. – Matt Oct 15 '14 at 17:55
  • Hi Matt,No I'm not. I need to be able to run this without elevation. Everything works perfectly fine except the "Exclude" part. Thanks. – Besiktas Oct 15 '14 at 17:59
  • Not the best solution but you could have it error silently `-ErrorAction SilentlyContinue`. It would still get the rest of the folder details. – Matt Oct 15 '14 at 18:06
  • That would work partially. But when it comes to the "users" folder it's going to start copying every office files which I don't want. – Besiktas Oct 15 '14 at 18:09

1 Answers1

0
$source = "C:\"
$destination = "C:\Backup"
$Exclude =  @("PerfLogs","Backup","boot","MSOCache","programdata","Recovery","TrueDelete","Users","Windows","Documents and Settings","Program Files \(x86\)\\Google\\","System Volume Information" ,'\$Recycle\.Bin',"Config\.Msi","bmm")
$includes ="*.xls " , "*.xlsx" , "*.doc" , "*.docx" , "*.ppt" , "*.pptx"
$regex = "($($Exclude -join "|"))"
$items = Get-ChildItem $source -Recurse -Include $includes -ErrorAction SilentlyContinue | 
        ?{ $_.fullname -notmatch $regex } #| Copy-Item -Destination $Destination -Container -Force

I dont think it will exclude the directories before reading them hence the error. A comprimise would be to read all directories and files that it can ( yes i know it is slow ) since it cannot run with elevated rights. On the files it is able to read it will use a Where-Object to -notmatch a regex string built from your $exclude variable. I had to escape some of the characters for regex as you will see. So any files that have those words in them will be excluded.

Caveat

As of this moment it would exclude backup.docx. If that is a concern you could edit "backup" in $Exclude to be "\\backup\\" to exclude files in that directory only. The \\ again is needed for regex since \ is a control character. You can uncomment the Copy-Item when you feel that it is working.

Testing Sample

Running this on my own computer unelevated returned 17 files from the following directories.

C:\Program Files (x86)\InstallShield\2014\Samples\InstallScript\Cumulative Setu...
C:\Program Files (x86)\InstallShield\2014\Samples\InstallScript\Platform Suites...
C:\Program Files (x86)\InstallShield\2014\Samples\InstallScript\Serial Number V...
C:\Program Files (x86)\LibreOffice 3.6\program\python-core-2.6.1\lib              
C:\Program Files (x86)\Microsoft Office\Office14\1033                             
C:\Program Files (x86)\Simon Sez IT\SharePoint Foundation 2013 Training\exercis...
C:\Temp    
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Hi Matt,Thank you for taking your time and helping me with this. But when i tested your above script, it wasn't excluding the folders such as "Users" the documents on my desktop were copied over to the destination folder, which shouldn't happen. – Besiktas Oct 15 '14 at 18:33
  • Ok, @Besiktas, I will have another look at it. – Matt Oct 15 '14 at 18:34
  • Did you copy my code _exactly_? I just tested this again to verify that the `Where-Object` was working and it skipped my desktop, documents, downloads and the `C:\Windows\ShellNew` directory. – Matt Oct 15 '14 at 19:24
  • Hi Matt,Sorry that was my mistake. I had a test folder that had my desktop items in for testing and it was picking it up from there. Thank you so much for your help. – Besiktas Oct 15 '14 at 19:48