Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Single line calculated property throws ParserError. Bug or not?

Have replicated this on two machines in both Windows PowerShell 5.1 and PowerShell Core 7.2.2, but seen no mention of it online. Is this a known bug, or should calculated properties simply not be written on a single line?

This fails with Unexpected token 'expression=' in expression or statement.

Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, @{ label='FreeSpace' expression={($_.FreeSpace/1GB).ToString('F2')}}

Adding a carriage return before expression= prevents the ParserError

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, @{ label='FreeSpace'
expression={($_.FreeSpace/1GB).ToString('F2')}}

>Solution :

Is this a known bug

No

should calculated properties simply not be written on a single line?

Sure, you just need an explicit statement terminator to separate the two key-value entries.

When you organize a hashtable literal (the @{<key>="<value>"} construct) with each key-value pair on separate lines, the line breaks act as implicit statement terminators.

Once you remove the line breaks, you need to use ; in their place:

# this ...
@{
  Label = 'FreeSpace'
  Expression = { ... }
}

# ... becomes this
@{ Label = 'FreeSpace'; Expression = { ... } }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading