Out-Default可以將對象轉換成可視的文本。事實上Out-Default會首先調用Format-Table,將更多的屬性默認隱藏。再調用Out-Host將結果輸出在控制臺上。因此下面的三組命令執行結果是相同的。
ls
ls | Format-Table | Out-Host
ls | Out-Default
顯示隱藏的對象屬性
要查看對象結果的所有屬性,可是使用
ls | Format-Table *
這樣因為屬性和屬性的內容太多可能不會顯示完全,可以使用文本換行參數
ls | Format-Table * -Wrap
格式化管道結果
首先可是使用下面的命令查看所有以Format打頭的命令
PS C:Powershell> Get-Command -Verb format
CommandType Name Definition
----------- ---- ----------
Cmdlet Format-Custom Format-Custom [[-Property]
Cmdlet Format-List Format-List [[-Property]
Cmdlet Format-Table Format-Table [[-Property]
Cmdlet Format-Wide Format-Wide [[-Property]
Format-Custom: 使用自定義視圖來設置輸出的格式。
Format-List: 將輸出的格式設置為屬性列表,其中每個屬性均各占一行顯示。
Format-Table: 將輸出的格式設置為表。
Format-Wide: 將對象的格式設置為只能顯示每個對象的一個屬性的寬表。
顯示指定的屬性
要顯示指定的屬性,你首先得知道結果對象中的屬性名,例如:
PS C:Powershell> ls | Format-Table Name,Length,LastWriteTime
Name Length LastWriteTime
---- ------ -------------
ABC 2011/11/23 17:25:53
myscript 2011/11/29 18:21:28
a.html 67580 2011/11/24 18:30:13
a.txt 26384 2011/11/24 20:04:31
alias 12060 2011/11/24 20:26:36
使用通配符
例如要查看當前以i打頭的進程,并顯示進程的名字和其它以”pe”打頭,以”64″結尾的進程。
PS C:Powershell> Get-Process i* | Format-Table Name,pe*64
Name PeakPagedMemorySize PeakWorkingSet64 PeakVirtualMemorySi
64 ze64
---- ------------------- ---------------- -------------------
Idle 0 0 0
IMECFMUI 946176 4292608 48054272
IMECMNT 1564672 5320704 65482752
IMEDICTUPDATE 1224704 4579328 31965184
腳本塊作為屬性
在Powershell中文件的Length默認以byte作為單位如果你象讓它輸出時以KB顯示,可是考慮羨下面的方法。
PS C:Powershell> ls | Format-Table Name,{ [int]($_.Length/1kb) }
Name [int]($_.Length/1kb)
---- ----------------------
function.ps1 21
LogoTestConfig.xml 0
ls.html 3
name.html 7
修改列標題
使用合成的屬性,如果使用腳本塊作為標題,看著很不爽。可以使用Lable設置。同樣是上面的例子,稍作修改。
PS C:Powershell> $column = @{Expression={ [int]($_.Length/1KB) }; Label="KB" }
PS C:Powershell> Dir | Format-Table Name, $column
Name KB
---- ----------------------
function.ps1 21
LogoTestConfig.xml 0
ls.html 3
name.html
優化列寬度
因為Powershell的絕大多數輸出都是實時的流模式,所以下一條結果的寬度未知,Powershell的結果會默認采用分散對齊,這樣可以最大限度利用控制臺的寬度,但是可以通過-auto參數對列的寬帶進行優化,會將屬性值的最大寬帶作為每一列的寬度,對比一下吧:
PS C:Powershell> ls
目錄: C:Powershell
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2011/11/23 17:25 ABC
d---- 2011/11/29 18:21 myscript
-a--- 2011/11/24 18:30 67580 a.html
-a--- 2011/11/24 20:04 26384 a.txt
PS C:Powershell> ls | Format-Table -AutoSize
目錄: C:Powershell
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2011/11/23 17:25 ABC
d---- 2011/11/29 18:21 myscript
-a--- 2011/11/24 18:30 67580 a.html
您可能感興趣的文章:- Windows Powershell使用管道
- Windows Powershell排序和分組管道結果
- Windows Powershell過濾管道結果
- Windows Powershell分析和比較管道結果
- Windows Powershell導出管道結果
- Windows Powershell擴展類型系統