Native Win32_Directory Scripting
I hope you've had an opportunity to try the code examples from the last few articles. I'm not implying that using CIM classes is necessarily the the best way to get file and folder information, but it might be a good alternative in some situations. Or if you have a very specific use case, it might be the best way to get the information you need. If nothing else, you should be able to learn from the code techniques in my examples.
There's nothing wrong with writing a function that uses Get-CimInstance
and Get-CimAssociatedInstance
. However, if you have the skills and information to take advantage of the native CIM classes, can eke out a little more performance.
The one thing to keep in mind is that if need to write Pester tests for your code, you can't mock a CIM class method. You would need to write a wrapper or helper function to abstract the CIM class method. That you could mock in a Pester test.
To recap, code like this should work.