Get-ChildItem Exclude and File parameters don't work together

痞子三分冷 提交于 2019-12-29 01:24:46

问题


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:

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

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

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

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

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?


回答1:


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

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

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

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)

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


来源:https://stackoverflow.com/questions/43191453/get-childitem-exclude-and-file-parameters-dont-work-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!