英文:
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}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论