How to use custom headers in the PowerShell output table?



If you want the specific properties in PowerShell, you need to use the Select-Object (Alias βˆ’ Select) as a pipeline.

In the below example, we will retrieve the specific properties of the Spooler service.

Example

Get-Service Spooler | Select Name, DisplayName, Starttype, Status

Output

Name       DisplayName    StartType    Status
----       -----------    ---------    ------
Spooler    Print Spooler  Automatic    Running

Now we will customize the header by renaming the β€œName” property to the β€œAlias Name” by the Name and the Expression syntax inside the Select-object.

Example

Get-
Service Spooler | Select @{N='Alias Name';E={$_.Name}}, DisplayName, Starttype, Status

You can also use other commands as well inside the expression.

Get-ComputerInfo | Select Csname, WindowsProductName, Csworkgroup

Output

CsName             WindowsProductName       CsWorkgroup
------             ------------------       -----------
DESKTOP-9435KM9    Windows 10 Pro           WORKGROUP

In the above example, we need to add property β€œComputer Uptime” and for that, we need to add new command to the existing command using Expression syntax.

Example

Get-
ComputerInfo | Select Csname, WindowsProductName, Csworkgroup, @{N='Days Uptime';E={((Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime).Days}}

Output

CsName             WindowsProductName             CsWorkgroup       Days Uptime
------             ------------------             -----------       -----------
DESKTOP-9435KM9    Windows 10 Pro                 WORKGROUP             1
Updated on: 2020-03-26T07:07:34+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements