Is “Dollar-sign” optional in powershell “$()”?

前端 未结 2 534
天涯浪人
天涯浪人 2021-01-26 20:06

I\'ve seen an example for setting the timestamps (creationtime, lastaccesstime, lastwritetime) of a file with powershell:

PS>$(get-item test.txt).lastwritetim         


        
相关标签:
2条回答
  • 2021-01-26 20:33

    You need the $ sign to denote a sub expression if you use multiple statements or the statement is embedded in a string. Parenthesis without the $ is just a grouping operator.

    $($x = 1; $y =2; $x + $y).tostring() //3
    
    ($x = 1; $y =2; $x + $y).tostring() // invalid syntax
    
    ($x = 1 + 2).tostring() //3
    
    "$(1 +2)"  //3
    
    0 讨论(0)
  • 2021-01-26 20:42

    The first time I need of $() was inside a string :

    $a = Get-Process "explorer"
    Write-host "$a.Name" -> System.Diagnostics.Process (explorer).Name
    Write-Host "$($a.Name)" -> explorer
    

    I also use it each time I want PowerShell to force compute something first.

    0 讨论(0)
提交回复
热议问题