Powershell: Function Interpolation

I am a beginner professional. What is the reason for using $ () to get Write-Host to evaluate this function? I could not find the 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
+3
source share
1 answer

You can remove the dollar sign and use only

Write-Host ([math]::round($diff.TotalMinutes, 2))

Brackets are necessary for the analyzer, so the expression is evaluated first, and then bound to the parameter -Object. There are some rules when a parser expects a string to be an expression, and when it treats it as a string and passes it to the command without evaluation. More information can be found in PowerShell in action .

Dollar sign needed if more expressions are shared ;

Write-Host $(get-date; 1; "test")
+4
source

Source: https://habr.com/ru/post/1790800/


All Articles