Variable $($user.something) or $user.something

Hi.

Bare with me as i am new to powershell and is still learning a lot :slight_smile:
I’ve come across code like this, the example is just a small snippet of a bigger script:
Example 1

$($User.SamAccountName)

Example 2

$User.SamAccountName

it returns the same thing, the value of $User.SamAccountName. So what is the difference? and why would you write the first example?

$() … this is the subexpression operator. You use it for example when you want to substitute a variable inside a string.
So something like this:

"This is a string with a variable: $User.SamAccountName"

or

"This is a string with a variable: $($User.SamAccountName)"

Here you have some more to read about:

https://ss64.com/ps/syntax-operators.html

1 Like

Once again thank you Olaf. :slight_smile:
It was hard to google something i did know know what was called, the information linked is excelent.

Thanks! :slight_smile: