5

Is & ldquo; Dollar-sign & rdquo; Optional in powershell & ldquo; $ (...

 3 years ago
source link: https://www.codesd.com/item/is-dollar-sign-optional-in-powershell.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Is & ldquo; Dollar-sign & rdquo; Optional in powershell & ldquo; $ () & Rdquo;

advertisements

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

PS>$(get-item test.txt).lastwritetime=$(get-date "01/01/2020")

which seems to work.

This page: http://ss64.com/ps/syntax-operators.html about powershell operators says that "$( )" is a "SubExpression operator".

But it also seems to work without the "$" like:

PS>(get-item test.txt).lastwritetime=(get-date "01/01/2020")

and most powershell examples I've seen omit the "$" when using parenthesis.

So, is the "Dollar-sign" optional in powershell "$()" ? Or is there some difference with/without the "$" that I'm just not seeing.

Is "$()" and "()" actually 2 different operators that just happen to both be valid uses in the examples I have shown?


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

Tags powershell

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK