Get-ChildItem Exclude 和 File 参数不能一起使用 [英] Get-ChildItem Exclude and File parameters don't work together

查看:28
本文介绍了Get-ChildItem Exclude 和 File 参数不能一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么 Get-ChildItem cmdlet 的这两个参数不能一起使用.为了让我的问题尽可能清楚,请看以下示例.从 Powershell ISE 命令窗格:

I can't figure out why these two parameters of the Get-ChildItem cmdlet don't work together. To make my question as clear as possible, look at the following example. From the Powershell ISE command pane:

  • 输入'dir' --> 显示当前目录下的所有文件和子文件夹.

  • Type 'dir' --> All files and sub-folders in the current directory are displayed.

输入'dir -File' --> 显示减去子文件夹的原始列表.

Type 'dir -File' --> Original list minus sub-folders is displayed.

键入 'dir -Exclude "*.txt"' --> 显示原始列表减去 .txt 文件.

Type 'dir -Exclude "*.txt"' --> Original list minus .txt files is displayed.

输入 'dir -File -Exclude "*.txt"' --> 没有显示任何内容.

Type 'dir -File -Exclude "*.txt"' --> NOTHING is displayed.

我希望原始列表减去子文件夹和 .txt 文件.但是无论我对-Exclude"使用什么参数,我都没有列出任何项目.我查看了 Get-ChildItem -full 文档和相关文章(堆栈溢出)以及其他可靠资源,但仍然不明白为什么会失败.当您添加-File"时,即使是经典的-Include '*.txt' -Exclude 'A*'"示例也会失败.如何同时使用 -File 和 -Exclude?

I would expect the original list minus sub-folders and .txt files. But regardless of what argument I use for '-Exclude', I get no items listed. I have looked at the Get-ChildItem -full documentation, and the related articles here (Stack Overflow) and at other reliable resources, and still don't understand why this fails. Even the classic "-Include '*.txt' -Exclude 'A*'" example fails when you add "-File". How can I use -File and -Exclude together?

推荐答案

虽然 dir 是 Get-ChildItem 的别名,但我发现在提供答案时最好使用完整的 cmdlet.

Whilst dir is an alias for Get-ChildItem, I find it best to use the full cmdlets when providing answers.

要使用正确的 PowerShell cmdlet,最好使用以下内容:

To use proper PowerShell cmdlets it would be best for you to use the following:

Get-ChildItem * -Exclude "*.txt" -File

您在上面看到的是 PowerShell cmdlet,用于获取指定路径中的所有项目(使用 * 假设您需要当前位置的所有项目)

What you see above is the PowerShell cmdlet to get all items in the path specified (using the * assumes you want all items from the current location)

您也可以使用 -Path 并提供您想要获取项目的路径的位置,例如:

You can also use -Path and provide the location of the path to where you want to get the items as well, such as:

Get-ChildItem -Path "C:\Path\Folder" -Exclude "*.txt" -File

这篇关于Get-ChildItem Exclude 和 File 参数不能一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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