Skip to content

Format-* cmdlets throw NullReferenceException when -Property is an empty array #26543

@yotsuda

Description

@yotsuda

Prerequisites

Steps to reproduce

When passing an empty array to the -Property parameter, Format-Table, Format-List, and Format-Custom throw a NullReferenceException. Format-Wide throws a NotSupportedException.

# Format-Table - throws NullReferenceException
Get-Process | Select-Object -First 1 | Format-Table -Property @()

# Format-List - throws NullReferenceException
Get-Process | Select-Object -First 1 | Format-List -Property @()

# Format-Custom - throws NullReferenceException
Get-Process | Select-Object -First 1 | Format-Custom -Property @()

# Format-Wide - throws NotSupportedException
Get-Process | Select-Object -First 1 | Format-Wide -Property @()

Expected behavior

Either:
1. Treat `-Property @()` the same as omitting `-Property` (display default properties), or
2. Display a clear, user-friendly error message indicating that at least one property must be specified


PS> Get-Process | Select-Object -First 1 | Format-Table -Property @()
# Option 1: Display default properties (same as without -Property)
# Option 2: Error message like "At least one property must be specified"

Actual behavior

PS> Get-Process | Select-Object -First 1 | Format-Table -Property @()
Format-Table: Object reference not set to an instance of an object.

Error details

PS> Get-Error
Exception             :
    Type       : System.NullReferenceException
    TargetSite :
        Name          : Initialize
        DeclaringType : Microsoft.PowerShell.Commands.Internal.Format.FormatViewManager
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message    : Object reference not set to an instance of an object.
    Source     : System.Management.Automation
    HResult    : -2147467261
    StackTrace :
   at Microsoft.PowerShell.Commands.Internal.Format.FormatViewManager.Initialize(TerminatingErrorContext
 errorContext, PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, PSObject so, FormatShape shape, FormattingCommandLineParameters parameters)
   at Microsoft.PowerShell.Commands.Internal.Format.InnerFormatShapeCommand.ProcessObject(PSObject so)
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Format-Table], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.FormatTableCommand
InvocationInfo        :
    MyCommand        : Format-Table
    ScriptLineNumber : 1
    OffsetInLine     : 46
    HistoryId        : 1
    Line             : try { Get-Process | Select-Object -First 1 | Format-Table -Property @() } catch { }
    Statement        : Format-Table -Property @()
    PositionMessage  : At line:1 char:46
                       + … Process | Select-Object -First 1 | Format-Table -Property @() } catch …
                       +                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Format-Table
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.4
PSEdition                      Core
GitCommitId                    7.5.4
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the productWG-Engine-Format

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions