How to use custom headers in the PowerShell output table?

PowerShellMicrosoft TechnologiesSoftware & Coding

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
raja
Published on 26-Mar-2020 11:36:19
Advertisements