How do I get only directories using Get-ChildItem?

后端 未结 15 1407
北海茫月
北海茫月 2020-11-29 16:30

I\'m using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can\'t figure out h

15条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 16:45

    You'll want to use Get-ChildItem to recursively get all folders and files first. And then pipe that output into a Where-Object clause which only take the files.

    # one of several ways to identify a file is using GetType() which
    # will return "FileInfo" or "DirectoryInfo"
    $files = Get-ChildItem E:\ -Recurse | Where-Object {$_.GetType().Name -eq "FileInfo"} ;
    
    foreach ($file in $files) {
      echo $file.FullName ;
    }
    

提交回复
热议问题