I am using the Get-ChildItem cmdlet with the -Recurse parameter to enumerate all files and directories in a specific path.
If I run : (Get-ChildItem -Path $Path -Recurse | Where-Object -Property Attributes -Value Directory -EQ).Directory, it returns $null.
If I access this same Directory property on a file object, this returns the relative path to the search path.
Why isn't the same behaviour observed with a directory object ?
The behavior differs because it's a different kind of object.
Files are represented by
FileInfoobjects (which happen to have aDirectoryproperty), whereas directories are represented byDirectoryInfoobjects (which do not happen to have such a property, but instead has aParentproperty for the same purpose).You can use
Get-Memberto discover these output types and their members:If you just want the path to the parent directory, use
Select-ObjectwithSplit-Path:It's worth noting that PowerShell's provider subsystem attaches an ETS property named
PSIsContainerto indicate whether a provider item is a container type (eg. a directory) or a leaf (eg. a file), so you don't need to inspect theAttributesproperty:The file system provider also exposes two parameters to
Get-ChildItemwith which you can filter up front: