I need to list existing directories without listing files into them :
PS C:\Users\myHomeDIR> cat .\toto.txt
.\.config
C:\DA FI
PS C:\Users\myHomeDIR> cat .\toto.txt | dir | % FullName
C:\Users\myHomeDIR\.config\scoop
dir : Cannot find path 'C:\DA FI' because it does not exist.
At line:1 char:18
+ cat .\toto.txt | dir | % FullName
+ ~~~
+ CategoryInfo : ObjectNotFound: (C:\DA FI:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
PS C:\Users\myHomeDIR>
I expect this output instead (like LINUX ls -d would) :
C:\Users\myHomeDIR\.config
dir : Cannot find path 'C:\DA FI' because it does not exist.
At line:1 char:18
+ cat .\toto.txt | dir | % FullName
+ ~~~
+ CategoryInfo : ObjectNotFound: (C:\DA FI:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Use
Get-Itemrather thanGet-ChildItem(one of whose built-in aliases isdir), as the former always reports the input items themselves, even if they're directories:Using its built-in
gialias, andgcrather thancatas aGet-Contentalias:As for the general question:
Get-Itemis the equivalent of Unixls -dLike
ls -d,Get-Itemreports whatever paths it is given as themselves, i.e. it doesn't list the content of directories among the input paths (the latter is whatlsdoes by default).However, instead of reporting strings (names/paths only by default),
Get-Itemreports rich objects (System.IO.FileInfoand/orSystem.IO.DirectoryInfoinstances) that allow flexible later processing (e.g., as shown in the question,% FullNameextracts the full paths via the.FullNameproperty, and.Namewould yield just the name).By contrast,
Get-ChildItem -Directory, while seemingly a closer analog, works differently:With literal input paths:
If a directory path is provided (by default: the current directory), it enumerates that directory's child directories - only, as themselves, i.e. without listing their content.
With file paths as input, it acts the same as without
-Directory(and, in this case, therefore likels -d)With wildcard expressions as input:
Only directories among the matching items are reported.
If you omit
-Directory, files are reported too - and matching directories are still reported as themselves.Get-ChildItemby default acts likels -d; e.g.,Get-ChildItem *basically acts the same asls -d *(except that the former emits rich objects, whereas the latter emits strings (names only)).