i try to get-childitems of Typ Directory and want to exclude dedicated Directorys from the result:
$FolderstoSkip = @(“c:\Windows”,“C:\Temp”)
$Folderlist += (Get-Childitem $Drive.Root -Recurse -Directory | where { — Folder is not part of the exluded Folders or an Subfolder of it…— })
But i struggle a bit with the phrase i should use here… i dont want to run through in a for-next loop… any idea will kindly appreciated…
Example:
Element in Get-Childitem is “C:\Windows\System32” what is a subfolder of “c:\windows” and have to been excluded…
This will be inefficient due to it checking every folder for a match even those folders you want to exclude. I think a simpler approach would be to get the root directory list first and then feed those to a recursive search. This will eliminate the processing of all child directories within the excluded directories.
I also added -ErrorAction and -ErrorVariable so the console remains clean as there are certainly going to be access denied to a few directories. You can inspect the variable $errors to see which directories resulted in error.
You can also clean the code up/make it more readable by using splatting.
This is also a good point. For this example it’s true. As Olaf points out, if you needed to start excluding folders that are deeper, his suggestion may be the best choice.
Amazing… you guys are awesome. thank you very much… Seems i have much to learn. And also thank you for the warm welcome…
In Fact, the Problem is, there are several subfolders to exclude so the inefficient way seems to be the best. i will run my script the next days agains the SAN machines and see how fast it will be.
Local Drive C only, Windows Folder excluded:
Olaf Methode 1: 18 seconds
krzydoug Methode 2: 8 seconds
SAN with about 500.000 Folders, mapped as Drive with exlusion of a rootfolder and a subfolder:
TotalMinutes Olaf Methode: 79,6190819866667
TotalMinutes krzydoug Methode: 64,8277968883333
TotalMinutes both Mixed: 66,5072204416667