My colleagues and I ran into a bit of a problem trying to find all the files that matched a pattern at or below the current folder.  The problem is that the PowerShell Get-ChildItem cmdlet has a bizarre notion of file and path matching.  Consider this cmd.exe command:

dir /s Microsoft*

This will return all the files that begin with the word Microsoft at or below the current directory.  You might think the following PowerShell command would do the same thing:

Get-ChildItem Microsoft* -Recurse

Well, guess again.  That returns all the contents of all the folders that begin with the word Microsoft.  Huh?  When do you ever need to do that?  Okay, maybe someone needs to do that sometimes, but for that to be the default is mind-boggling.  Try this instead:

Get-ChildItem -Include Microsoft -Recurse

This command does the same thing as the above cmd.exe command.  It’s rather bizarre why that isn’t the default when recursing, but at least there is a fairly low-cost way to do it.

Resources