In general: Get-ChildItem has the parameters -File and -Directory. With these you can limit the output to either files or folders and you do not have to filter them later on with a Where-Object.
Inside your loop you use an if condition and the -contains operator. This operator checks if an element is in a list. But this element has to be exactly the same in the list. So it will not work if you check a complete folder path and you only have parts of the folder path in your list.
and when I run the loop, I get the following output.
'Incoming' is in array
'Outgoing' is in array
'test' is in array
'incoming' is in array
'outgoing' is in array
'TEST' is in array
'TEST' is in array
'Incoming' is in array
'Outgoing' is in array
So the “if” part of my loop seems to be working, but the “else” part is not.
Since you said the first 2 steps work for you we only focus on the last one. I’d recommend to exclude the desired folders already when querying the list. … like this: