如何使用 Get-ChildItem 仅获取目录?
我正在使用 PowerShell 2.0,我想通过管道输出某个路径的所有子目录。以下命令输出所有文件和目录,但我不知道如何过滤掉文件。
Get-ChildItem c:\mypath -Recurse
我尝试使用 $_.Attributes
来获取属性,但我不知道如何构造 System.IO.FileAttributes
的文字实例来将其与。在 cmd.exe
中,它将是
dir /b /ad /s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
对于 PowerShell 3.0 及更高版本:
您还可以使用别名
dir
、ls
和gci
对于PowerShell 版本低于 3.0:
Get-ChildItem
返回的FileInfo
对象具有一个“基本”属性PSIsContainer
。您只想选择这些项目。如果您想要目录的原始字符串名称,您可以这样做
For PowerShell 3.0 and greater:
You can also use the aliases
dir
,ls
, andgci
For PowerShell versions less than 3.0:
The
FileInfo
object returned byGet-ChildItem
has a "base" property,PSIsContainer
. You want to select only those items.If you want the raw string names of the directories, you can do
在PowerShell 3.0中,更简单:
In PowerShell 3.0, it is simpler:
使用
如果您更喜欢别名,请使用
或
要递归子目录,请添加
-r
选项。已在 PowerShell 4.0、PowerShell 5.0 (Windows 10) 上测试,PowerShell Core 6.0(Windows 10、Mac 和 Linux)和 PowerShell 7.0(Windows 10、Mac 和 Linux)。
注意:在 PowerShell Core 上,当您指定
-r
开关时,不会遵循符号链接。要跟踪符号链接,请使用-r
指定-FollowSymlink
开关。注 2:从 6.0 版开始,PowerShell 现在是跨平台的。跨平台版本最初称为 PowerShell Core,但自 PowerShell 7.0+ 以来,“Core”一词已被删除。
Get-ChildItem 文档: https://learn .microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
Use
If you prefer aliases, use
or
To recurse subdirectories as well, add
-r
option.Tested on PowerShell 4.0, PowerShell 5.0 (Windows 10), PowerShell Core 6.0 (Windows 10, Mac, and Linux), and PowerShell 7.0 (Windows 10, Mac, and Linux).
Note: On PowerShell Core, symlinks are not followed when you specify the
-r
switch. To follow symlinks, specify the-FollowSymlink
switch with-r
.Note 2: PowerShell is now cross-platform, since version 6.0. The cross-platform version was originally called PowerShell Core, but the the word "Core" has been dropped since PowerShell 7.0+.
Get-ChildItem documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
更简洁的方法:
我想知道 PowerShell 3.0 是否有一个仅返回目录的开关;添加这似乎是合乎逻辑的事情。
A cleaner approach:
I wonder if PowerShell 3.0 has a switch that only returns directories; it seems like a logical thing to add.
使用:
Use:
从 PowerShell v2 及更高版本(k 代表您开始搜索的文件夹):
如果您只需要文件夹名称,而不需要其他内容,请使用此命令:
如果您正在查找特定文件夹,您可以使用以下命令。在本例中,我正在查找名为
myFolder
的文件夹:From PowerShell v2 and newer (k represents the folder you are beginning your search at):
If you just want folder names only, and nothing else, use this:
If you are looking for a specific folder, you could use the following. In this case, I am looking for a folder called
myFolder
:这种方法需要更少的文本:
Less text is required with this approach:
接受的答案提到
获取“原始字符串”。
但实际上将返回 Selected.System.IO.DirectoryInfo 类型的对象。对于原始字符串,可以使用以下内容:
如果将值连接到字符串,则差异很重要:
Select-Object
令人惊讶的是foo\@{FullName=bar}
ForEach
- 预期的运算符:foo\bar
The accepted answer mentions
to get a "raw string".
But in fact objects of type
Selected.System.IO.DirectoryInfo
will be returned. For raw strings the following can be used:The difference matters if the value is concatenated to a string:
Select-Object
suprisinglyfoo\@{FullName=bar}
ForEach
-operator the expected:foo\bar
使用:
这将为您提供根结构的输出,其中仅包含目录的文件夹名称。
Use:
This will give you an output of the root structure with the folder name for directories only.
您需要使用 Get-ChildItem 首先递归获取所有文件夹和文件。然后将该输出传输到仅获取文件的 Where-Object 子句中。
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.
使用:执行
以下操作
Get-ChildItem \\myserver\myshare\myshare\ -Directory
Select-Object -Property name
convertto-csv -NoTypeInformation
输出文件 c:\temp\mydirectorylist.csv
Use:
Which does the following
Get-ChildItem \\myserver\myshare\myshare\ -Directory
Select-Object -Property name
convertto-csv -NoTypeInformation
Out-File c:\temp\mydirectorylist.csv
使用下面的脚本可以实现更具可读性和简单的方法:
希望这会有所帮助!
A bit more readable and simple approach could be achieved with the script below:
Hope this helps!
我的解决方案基于 TechNet 文章您可以使用 Get 做的有趣事情-ChildItem Cmdlet。
我在我的脚本中使用了它,并且效果很好。
My solution is based on the TechNet article Fun Things You Can Do With the Get-ChildItem Cmdlet.
I used it in my script, and it works well.
这个问题得到了很好的、真实的回答,但我想我应该添加一些额外的东西,因为我刚刚一直在看这个。
Get-ChildItem
恰好生成两种类型的对象,而大多数命令只生成一种。返回FileInfo和DirectoryInfo。
您可以通过查看此命令可用的“成员”来查看这一点,如下所示:
您将看到每种类型可用的各种方法和属性。请注意,存在差异。例如,FileInfo 对象具有 length 属性,但 DirectoryInfo 对象没有。
无论如何,从技术上讲,我们可以通过隔离 DirectoryInfo 对象来仅返回目录
显然,正如最佳答案所述,最直接的解决方案是简单地使用
Get-ChildItem -Directory
但我们现在知道如何使用多个未来的对象类型:)This question is well and truly answered but thought I'd add something extra as I've just been looking at this.
Get-ChildItem
happens to produce two types of objects whereas most commands produce just one.FileInfo and DirectoryInfo are returned.
You can see this by viewing the 'members' available to this command like so:
You'll see the various methods and properties available to each type. Note that there are differences. For example that the FileInfo object has a length property but the DirectoryInfo object doesn't.
Anyway, technically, we can return just the directories by isolating the DirectoryInfo object
Obviously as the top answer states the most straightforward solution is to simply use
Get-ChildItem -Directory
but we now know how to work with multple object types in future :)使用这个:
Use this one:
您可以尝试 PsIsContainer 对象
You can try the PsIsContainer Object
要具体回答原始问题(使用 IO.FileAttributes ):
不过,我确实更喜欢 Marek 的解决方案:
To answer the original question specifically (using
IO.FileAttributes
):I do prefer Marek's solution though:
尝试以下方法
dir -目录 -名称
TRY THE FOLLOWING
dir -Directory -Name
这是我使用的方法,它为您提供了目录和文件名:
Here is what I use which gives you both the directory and the filename: