I'm having a tough time figuring out how to write an IF/ELSE statement to check if folders in a System.Array contain any documents. I have a structure that includes folders labeled as having documents or not, like this:
- TEST CONTAINER
- FOLDER = Documents | HAS_DOCUMENTS = (True)
- FOLDER = Misc | HAS_DOCUMENTS = (False)
I need to determine if a folder has any documents, and if it doesn't, I want to send a delete request. I've tried various IF/ELSE examples from different resources, but I'm getting discrepancies when I try to actually add or remove documents after checking the folders.
In my function 'getFolder', I've aimed to simplify the process by only retrieving folders with a 'has_documents' property that equals true. I'm convinced it's something with my IF statement because it's always falling back to the ELSE branch. I could really use some insight on where I might be going wrong!
3 Answers
When comparing against $NULL, it's better to put $NULL on the left side of the comparison. This can help ensure you get the expected results. Check out Microsoft's documentation on this to learn more about possible incorrect comparisons. It might save you from unexpected outcomes!
You could utilize `Get-ChildItem` effectively here. If you want to check if a directory contains any files or subfolders, this command can simplify that task. Just remember to filter based on the `PSIsContainer` property when you want to differentiate between files and folders!
Instead of using '-contains' on the strings, consider using regular expressions with '-match'. This way, you can check if a specific substring like 'True' appears in your lines. For example, if your lines look like `|-- FOLDER = Documents | HAS_DOCUMENTS = (True)`, you can filter them more efficiently!

Thanks! I actually am getting folder properties via an API call, not checking local folders. I did try to implement a counter to count the folders with documents, but it keeps returning zero. Any ideas on that?