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

Categories

0 votes
947 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...