If I issue following command in PowerShell, I get a lot of rows back.
如果我在PowerShell中发出以下命令,我会收到很多行。
PS C:\Users\benh> get-command
CommandType Name ModuleName Definition
----------- ---- ---------- ----------
Cmdlet Get-Variable Microsoft.PowerShell.Utility Get-Variable...
Cmdlet Get-WebAppDomain WebAdministration Get-WebAppDomain...
Cmdlet Get-WebApplication WebAdministration Get-WebApplication...
Cmdlet Get-WebAppPoolState WebAdministration Get-WebAppPoolState...
...
Cmdlet Get-WinEvent Microsoft.PowerShell.Diagnostics Get-WinEvent...
Cmdlet Get-WmiObject Microsoft.PowerShell.Management Get-WmiObject...
Cmdlet Get-WSManCredSSP Microsoft.WSMan.Management Get-WSManCredSSP...
Cmdlet Get-WSManInstance Microsoft.WSMan.Management Get-WSManInstance...
Cmdlet Group-Object Microsoft.PowerShell.Utility Group-Object...
Cmdlet Import-Alias Microsoft.PowerShell.Utility Import-Alias...
Cmdlet Import-Clixml Microsoft.PowerShell.Utility Import-Clixml...
Cmdlet Import-Counter Microsoft.PowerShell.Diagnostics Import-Counter...
Cmdlet Import-Csv Microsoft.PowerShell.Utility Import-Csv...
Cmdlet Import-LocalizedData Microsoft.PowerShell.Utility Import-LocalizedData...
Cmdlet Import-Module Microsoft.PowerShell.Core ...
What I want to do is get all the distinct ModuleNames returned by Get-Command. How can I do this with PowerShell?
我想要做的是获取Get-Command返回的所有不同的ModuleNames。如何使用PowerShell执行此操作?
In pseudo-C#:
在伪C#中:
PowerShell.Exec("Get-Command").Select(a=> a.ModuleName).Distinct();
Thanks in advance!
提前致谢!
4 个解决方案
#1
60
Have you tried something like this?
你尝试过这样的事吗?
get-command | select ModuleName | sort-object -Property ModuleName -Unique
#2
55
Even shorter:
更短:
get-command | select-object moduleName -unique
#3
4
Another option:
另外一个选择:
Get-Command | Group-Object ModuleName -NoElement | Select-Object Name
#4
2
Below 2 commands will bring out same result, but the first one will be sorted and a bit expensive in execution time.
低于2的命令将带来相同的结果,但第一个命令将被排序并且执行时间有点昂贵。
The execution time will be taken more into consideration when you have large number of items, For example if you are importing csv
file of 30,000 rows. Then second option will be faster, and once you get unique values sort them if you need because here sorting will be done on a much lesser number of items hence better performance.
当您有大量项目时,将更多地考虑执行时间,例如,如果要导入30,000行的csv文件。然后第二个选项会更快,一旦你获得了唯一的值,你可以根据需要对它们进行排序,因为这里的排序将在更少的项目上完成,因此性能更好。
1.
1。
get-command | select ModuleName | sort-object -Property ModuleName -Unique
# This will give you the execution time
Measure-Command {get-command | select ModuleName | sort-object -Property ModuleName -Unique}
2.
2。
get-command | select ModuleName -Unique
# This will give you the execution time
Measure-Command {get-command | select ModuleName -Unique}
#1
60
Have you tried something like this?
你尝试过这样的事吗?
get-command | select ModuleName | sort-object -Property ModuleName -Unique
#2
55
Even shorter:
更短:
get-command | select-object moduleName -unique
#3
4
Another option:
另外一个选择:
Get-Command | Group-Object ModuleName -NoElement | Select-Object Name
#4
2
Below 2 commands will bring out same result, but the first one will be sorted and a bit expensive in execution time.
低于2的命令将带来相同的结果,但第一个命令将被排序并且执行时间有点昂贵。
The execution time will be taken more into consideration when you have large number of items, For example if you are importing csv
file of 30,000 rows. Then second option will be faster, and once you get unique values sort them if you need because here sorting will be done on a much lesser number of items hence better performance.
当您有大量项目时,将更多地考虑执行时间,例如,如果要导入30,000行的csv文件。然后第二个选项会更快,一旦你获得了唯一的值,你可以根据需要对它们进行排序,因为这里的排序将在更少的项目上完成,因此性能更好。
1.
1。
get-command | select ModuleName | sort-object -Property ModuleName -Unique
# This will give you the execution time
Measure-Command {get-command | select ModuleName | sort-object -Property ModuleName -Unique}
2.
2。
get-command | select ModuleName -Unique
# This will give you the execution time
Measure-Command {get-command | select ModuleName -Unique}