Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:PathFolder" -Exclude "*.txt" -File

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...