如何在PowerShell中选择Property的最后一项?

huangapple go评论68阅读模式
英文:

How can I pick the last item of Property in powershell?

问题

以下是我的代码:

Add-Type -AssemblyName PresentationCore
[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -Property Source,FamilyNames

实际上,属性FamilyNames包含许多项,我只想选择最后一个。

我该如何实现这个目标?

英文:

Here is my code:

Add-Type -AssemblyName PresentationCore
[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -Property Source,FamilyNames

Actually, the property FamilyNames has many items and I only want to pick the last one.

How can I achieve this?

答案1

得分: 2

不要列出最后的SystemFontFamilies,正如你在评论中所述。你似乎想选择Source属性和FamilyNames中的最后一个条目。如果你检查FamilyNames中包含的内容,你会看到它是一个字典对象。

[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -expandProperty FamilyNames | Get-Member

TypeName: System.Windows.Media.LanguageSpecificStringDictionary

字典将包含键和值。如果你检查键/值,你应该会看到类似这样的内容。

[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -expandProperty FamilyNames

Name                           Value
----                           -----
en-us                          Cascadia Code
en-us                          Cascadia Mono

看起来你想选择Keys中的最后一个值,因此我会使用计算属性来提取所需的信息。

[Windows.Media.Fonts]::SystemFontFamilies |
    Select-Object -Property Source,
                            @{n='FamilyNames';e={$_.FamilyNames.Keys | Select-Object -Last 1}}
英文:

You do not want to list the last SystemFontFamilies, like you stated in your comment. You seem to want to select the property Source and the last entry in FamilyNames. If you examine what's contained within FamilyNames, you'll see it's a dictionary object.

[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -expandProperty FamilyNames | Get-Member

TypeName: System.Windows.Media.LanguageSpecificStringDictionary

A dictionary will contain keys and values. If you inspect the keys/values, you should see something like this.

[Windows.Media.Fonts]::SystemFontFamilies | Select-Object -expandProperty FamilyNames

Name                           Value
----                           -----
en-us                          Cascadia Code
en-us                          Cascadia Mono

It seems you want to select the last value in Keys so I would use a calculated property to pull the desired info

[Windows.Media.Fonts]::SystemFontFamilies |
    Select-Object -Property Source,
                            @{n='FamilyNames';e={$_.FamilyNames.Keys | Select-Object -Last 1}}

huangapple
  • 本文由 发表于 2023年4月11日 13:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定