如何在powershell中获取目录中不同文件的数量?

use*_*638 4 powershell powershell-4.0

我目前有这个代码,它返回文件总数,但我不想计算这个数字计数中的多个文件?

让我说我有这个:

01Red.txt
01Blue.txt
02Red.txt
05Red.txt
05Green.txt

Get-ChildItem -File *.txt -Path "C:\Users\Test\Desktop\TestDirectory" | Measure-Object | %{$_.Count}
Run Code Online (Sandbox Code Playgroud)

我想基于01,02,05返回总计数3但是我的代码我得到5.

如何让它返回3并忽略字符串中前2个字符后的所有内容?

Bil*_*art 7

我可能会建议Group-Object:

Get-ChildItem *.txt | Group-Object { $_.Name.Substring(0,2) }
Run Code Online (Sandbox Code Playgroud)

添加| Measure-Object以计算分组数(在您的示例中,这将是3).


aqu*_*nas 5

Get-ChildItem -File *.txt -Path "C:\Users\Test\Desktop\TestDirectory" 
| select {$_.BaseName.Substring(0,2)} | Get-Unique -AsString | measure
Run Code Online (Sandbox Code Playgroud)