powershelllsequivalent

How to do a ls -d in powershell


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


Solution

  • Use Get-Item rather than Get-ChildItem (one of whose built-in aliases is dir), as the former always reports the input items themselves, even if they're directories:

    Using its built-in gi alias, and gc rather than cat as a Get-Content alias:

    gc .\toto.txt | gi | % FullName
    

    As for the general question: