在PowerShell中调用Sort-Object而不使用管道

Seb*_*ski 2 powershell pipe

我是PowerShell的新手,我只想弄清楚它是如何工作的.

那么,我该如何编写这段代码:

Get-ChildItem C:\ | Sort-Object Length
Run Code Online (Sandbox Code Playgroud)

作为多行代码?我试过这个:

$child_items = Get-ChildItem C:\
Sort-Object $child_items Length
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我越来越:

Sort-Object : A positional parameter cannot be found that accepts argument 'Length'.
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 5

虽然其他答案是使用命名参数传递值的正确方法,但请记住get-help Sort-Object:

When you use the InputObject parameter to submit a collection of items, 
Sort-Object receives one object that represents the collection. 
Because one object cannot be sorted, Sort-Object returns the entire collection unchanged.
Run Code Online (Sandbox Code Playgroud)

你会发现没有将$ child_items传递给的排序操作-inputobject.

您始终需要使用管道传递值 -inputobject

  • ...如果你不喜欢它,你可以投票给这个连接项:http://connect.microsoft.com/PowerShell/feedback/details/568524/sort-object-does-not-sort-items-in -collections-传入-透inputobject参数 (3认同)