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


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


Example 2


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"


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

Here you have some more to read about:

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: