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!