這是我正在呼叫的命令:
Get-ChildItem | where -Property name -like *asda* | select -first 1 | $_.Name
顯然,最后的這個呼叫不起作用,因為$_它只適用于可迭代回圈。但是我想選擇串列中的那個元素并將它變成一個物件,我可以在其中呼叫自動完成(ctrl 空格)。
如何在 Powershell 中實作這一目標?
uj5u.com熱心網友回復:
你試試?
(Get-ChildItem | where -Property name -like *asda* | select -first 1).Name
uj5u.com熱心網友回復:
JPBlanc 的回答是有效的,但讓我深入挖掘一下:
對于流解決方案- 每個被發出的物件在發出時被一個一個Get-ChildItem處理- 使用與( ) cmdlet相同的簡化語法呼叫:ForEach-ObjectWhere-Objectwhere
Get-ChildItem |
Where-Object -Property Name -like *asda* |
Select-Object -First 1 |
ForEach-Object -MemberName Name
注意:或者,您可以使用.Select-Object -ExpandProperty Name
對于更簡潔的解決方案-其中涉及收集所有Get-ChildItem輸出前面-使用以下內容:
((Get-ChildItem).Name -like '*asda*')[0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389632.html
標籤:电源外壳
