Skip to content

Since Powershell 7.4, [PSCustomObject] typecast results in a null value.  #20934

@dam-pav

Description

@dam-pav

Prerequisites

Steps to reproduce

It would appear [PSCustomObject] is broken. I realized this because a lot of code stopped working after upgrading to 7.4. In 5.1 same code still works. Consider this test:

Clear-Host

Write-Host ''
Write-Host 'Hash:' -ForegroundColor Green
$newHash = @{
    AppId        = '12341234'
    Version      = '23452345'
    Name         = 'whatever'
    Publisher    = 'default'
}
$newHash


Write-Host ''
Write-Host 'Object with [PSCustomObject]:' -ForegroundColor Green
$newObject = [PSCustomObject]$newHash
$newObject


Write-Host ''
Write-Host 'Object with New-Object:' -ForegroundColor Green
$newObject = New-Object -TypeName psobject -Property $newHash
$newObject


Write-Host 'Done.' -ForegroundColor Green

Expected behavior

Hash:

Name                           Value
----                           -----
AppId                          12341234
Publisher                      default
Name                           whatever
Version                        23452345

Object with [PSCustomObject]:

AppId     : 12341234
Publisher : default
Name      : whatever
Version   : 23452345


Object with New-Object:

AppId     : 12341234
Publisher : default
Name      : whatever
Version   : 23452345

Done.

Actual behavior

Hash:

Name                           Value
----                           -----
Version                        23452345
Publisher                      default
AppId                          12341234
Name                           whatever

Object with [PSCustomObject]:

Object with New-Object:

Version   : 23452345
Publisher : default
AppId     : 12341234
Name      : whatever

Done.

Error details

No response

Environment data

Both environments:
PS C:\Projects\__KB\Docker> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.2600
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22000.2600
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Projects\__KB\Docker> $PSVersionTable

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

Visuals

Results in 5.1:
image

Results in 7.4:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-ReproThe issue author needs to provide repro steps.Resolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions