如何使用 PowerShell 輸出表中的自定義標題?
如果你想要在 PowerShell 中的特定屬性,你需要使用 Select-Object(別名 − Select)作為管道。
在下面的示例中,我們將檢索 Spooler 服務的特定屬性。
示例
Get-Service Spooler | Select Name, DisplayName, Starttype, Status
輸出
Name DisplayName StartType Status ---- ----------- --------- ------ Spooler Print Spooler Automatic Running
現在,我們將透過 Select-object 中的 Name 和表示式語法將“Name”屬性重新命名為 “Alias Name”,來自定義標題。
示例
Get- Service Spooler | Select @{N='Alias Name';E={$_.Name}}, DisplayName, Starttype, Status
你也可以在表示式內使用其他命令。
Get-ComputerInfo | Select Csname, WindowsProductName, Csworkgroup
輸出
CsName WindowsProductName CsWorkgroup ------ ------------------ ----------- DESKTOP-9435KM9 Windows 10 Pro WORKGROUP
在上述示例中,我們需要新增屬性 “Computer Uptime”, dafür,我們需要使用 Expression 語法向現有命令新增新命令。
示例
Get- ComputerInfo | Select Csname, WindowsProductName, Csworkgroup, @{N='Days Uptime';E={((Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime).Days}}
輸出
CsName WindowsProductName CsWorkgroup Days Uptime ------ ------------------ ----------- ----------- DESKTOP-9435KM9 Windows 10 Pro WORKGROUP 1
廣告