Home > database >  PowerShell 5.1 using ExpandedProperty but not getting desired output
PowerShell 5.1 using ExpandedProperty but not getting desired output

Time:01-06

Given:
PowerShell 5.1

If I run the following:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit*

I get this:

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  BDESVC             BitLocker Drive Encryption Service    
Stopped  Check Point Bit... Check Point Endpoint Security Bitlo...

But I want to expand both Name and DisplayName, so I do this:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Select -ExpandProperty Name 

and get this:

BDESVC
Check Point Bitlocker Management

How do I get all the columns with column headers, like the first output above except without ellipses?

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  BDESVC             BitLocker Drive Encryption Service    
Stopped  Check Point Bit... Check Point Endpoint Security Bitlo...

CodePudding user response:

The ... are just a display problem (the data is there in full).

If you use Select-Object's -ExpandProperty parameter, you by definition only get property values, and since these values are strings in your case, that's all you get (albeit without truncation).

To prevent the for-display truncation, pipe to Format-Table -AutoSize, though note that this requires PowerShell to collect all output from the input command first, before starting to display output (see next section for alternatives).

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Format-Table AutoSize

Note: Since the console (terminal) window width is always the limiting factor, -AutoSize can result in fewer columns getting displayed.

If possible, you can exclude columns that aren't of interest, by specifying only those that are, via -Property (you may omit this parameter name, becasue it is positionally implied); e.g.:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* |
  Format-Table -Property Status, Name -AutoSize

Alternatives that avoid -AutoSize and its collect-everything-first behavior:

  • Use -Wrap, which doesn't truncate, but uses (artificial) line breaks to spread values that don't fit into the column across multiple lines.

    Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Format-Table -Wrap
    
  • If you know the maximum width of a column of interest ahead of time, you can use a calculated property to prescribe a column's width; e.g.:

    Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* |
      Format-Table Status, 
                   @{ Expression='Name'; Width=40 },
                   DisplayName
    

CodePudding user response:

Get-Service -DisplayName *wmi*
Status   Name               DisplayName
------   ----               -----------
Stopped  wmiApSrv           WMI Performance Adapter
Running  WMIRegistrationSe… Intel(R) Management Engine WMI Provid…

I find myself using | Out-GridView if desired output isn't being shown.

Get-Service -DisplayName *wmi* | 
    Out-GridView

Out-GridView Result

Alternatively, you could use | Format-List if you're not set on using Table View

Get-Service -DisplayName *wmi* | 
    Format-List
Name                : wmiApSrv
DisplayName         : WMI Performance Adapter
Status              : Stopped
DependentServices   : {}
ServicesDependedOn  : {}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : False
ServiceType         : Win32OwnProcess

Name                : WMIRegistrationService
DisplayName         : Intel(R) Management Engine WMI Provider Registration
Status              : Running
DependentServices   : {}
ServicesDependedOn  : {}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
ServiceType         : Win32OwnProcess

If you only want Status,Name,DisplayName headers,

Get-Service -DisplayName *wmi* | 
    Select-Object -Property Status, Name, DisplayName | 
        Format-List
Status      : Stopped
Name        : wmiApSrv
DisplayName : WMI Performance Adapter

Status      : Running
Name        : WMIRegistrationService
DisplayName : Intel(R) Management Engine WMI Provider Registration

  • Related