如何使用 Get-ChildItem 仅获取目录? [英] How do I get only directories using Get-ChildItem?

查看:44
本文介绍了如何使用 Get-ChildItem 仅获取目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PowerShell 2.0,我想输出某个路径的所有子目录.以下命令输出所有文件和目录,但我不知道如何过滤掉文件.

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 how to filter out the files.

Get-ChildItem c:\mypath -Recurse

我尝试使用 $_.Attributes 来获取属性,但后来我不知道如何构造 System.IO.FileAttributes 的文字实例来把它比作.在 cmd.exe 中它会是

I've tried using $_.Attributes to get the attributes but then I don't know how to construct a literal instance of System.IO.FileAttributes to compare it to. In cmd.exe it would be

dir /b /ad /s

推荐答案

对于低于 3.0 的 PowerShell 版本:

Get-ChildItem 返回的 FileInfo 对象有一个base"属性,PSIsContainer.您只想选择这些项目.

The FileInfo object returned by Get-ChildItem has a "base" property, PSIsContainer. You want to select only those items.

Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

如果你想要目录的原始字符串名称,你可以做

If you want the raw string names of the directories, you can do

Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName

对于 PowerShell 3.0 及更高版本:

Get-ChildItem -Directory

您还可以使用别名 dirlsgci

You can also use the aliases dir, ls, and gci

这篇关于如何使用 Get-ChildItem 仅获取目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆