Powershell: My bug report

Created on 1 Oct 2019  路  2Comments  路  Source: PowerShell/PowerShell

Attempting to expand a calculated property results in the property not being found.

Steps to reproduce

Get-ChildItem C:\Test | Select-Object Name, @{Name="KB";Expression={$_.length / 1kb}} -ExpandProperty KB

Expected behavior

0.013671875

Actual behavior

Select-Object : Property "KB" cannot be found.
    + CategoryInfo          : InvalidArgument: (test.txt:PSObject) [Select-Object], PSArgumentException
    + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Environment data

PSVersion                      5.1.17134.858
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.858
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Issue-Question

All 2 comments

The property specified for -ExpandProperty must exist on the _input_ object - you cannot define a new calculated property and expand it _in the same call_.

You can pipe to _another_ Select-Object call in which you can perform the expansion (or, if you ultimately don't need the Name property, just use a ForEach-Object call in which you return the KB value).

P.S.: Please also give your issues a meaningful _title_ in the future.

This is very helpful. Thank you

Was this page helpful?
0 / 5 - 0 ratings