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
Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, @{ label='FreeSpace'
expression={($_.FreeSpace/1GB).ToString('F2')}}
CodePudding user response:
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 = { ... } }