Sunday, February 25, 2018

The -File and -Directory switches on Get-ChildItem didn't always exist

The Get-ChildItem cmdlet has -File and -Directory switches that limit the results to files and subdirectories, respectively. These are very nice, but I found when writing scripts for people on older versions of Windows (specifically Windows 7 or earlier) that they didn't always exist. To support those versions, you have to use the PSIsContainer property. So this is equivalent to gci -File:

gci | ? { -not $_.PSIsContainer }

For directories, of course, the PSIsContainer property is true.

No comments:

Post a Comment