0
votes

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!

1
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
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
Not the best solution but you could have it error silently -ErrorAction SilentlyContinue. It would still get the rest of the folder details.Matt
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

1 Answers

0
votes
$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