John Casablanca
John Casablanca

Reputation: 79

How to do inline math operation during "add-member" in Powershell

I am trying to do a math operation in a add-member line in Powershell, by dividing the value of a object property by 10. This is the code

This is my code

Foreach($item in $Collection)
{
    $Result = new-object PSObject
    $Result| add-member -membertype NoteProperty -name "Column1" -Value $item.Value/10
    $ResultSet += $Result
}

This gives me following error:

Add-Member : Cannot bind parameter 'MemberType'. Cannot convert value "/10" to type "System.Management.Automation.PSMemberTypes".

How can I fix this?

Upvotes: 0

Views: 752

Answers (1)

Seth
Seth

Reputation: 1255

The argument for -Value needs to be in brackets. Otherwise it's not clear what you're trying to do. For instance you could be trying to run Add-Member and trying to divide it by 10. The reason the brackets work is because they are (in most cases) evaluated first. So PowerShell will first compute the value for the division and use the computed value as the argument for value. Because it's a single value at that point it becomes clear what argument is to be supplied to the Value parameter.

Add-Member -MemberType NoteProperty -Name "Column1" -Value ($item.Value/10)

Upvotes: 2

Related Questions