I'm a PowerShell beginner. What is the reason that one should use $()
to get Write-Host to evaluate this function?
I could not 开发者_如何学Gofind a reason for this from the documentation.
PS C:\Temp> Write-Host [math]::round($diff.TotalMinutes, 2)
[math]::round 751681,102679735 2
___________________________________________________________________________________________________________
PS C:\Temp> Write-Host $([math]::round($diff.TotalMinutes, 2))
751681,1
You can remove the dollar sign and use just
Write-Host ([math]::round($diff.TotalMinutes, 2))
The brackets are needed for the parser so that first the expression is evaluated and then bound to parameter -Object
.
There are some rules when parser expects that the string will be expression and when it treats it as a string and passes it to the command without evaluating. More info can be found in PowerShell in Action.
Dollar sign is needed if there are more expressions separated by ;
Write-Host $(get-date; 1; "test")
精彩评论