Author Posts

May 18, 2017 at 2:53 pm

This doesn't work...

$Username = "dweatherston"
set-aduser $Username -HomeDirectory "\\domain\Users\site\$Username" -HomeDrive u:

But this does

set-aduser dweatherston -HomeDirectory "\\domain\Users\site\$Username" -HomeDrive u:

Why can't I use a variable next to set-aduser?

May 18, 2017 at 4:28 pm

Hey there! I just tested this in my environment and I'm able to get this to work. Is it possible that in your variable, something is misspelled, or you're inputting a user that doesn't exist?

May 18, 2017 at 4:31 pm

in cases like this, I usually encapsulate the parameter in $(), so $($Username)

May 18, 2017 at 6:31 pm

Adding $($Username) resolved the issue. However, can you explain what $() does?

May 18, 2017 at 6:39 pm

Basically it evaluates the value inside of the expression rather than the expression itself. Take the following code for instance:

$array = @("a","b","c")
$output = "$arr.length"
Write-Output $output

Is going to give you vastly different results than:

$array = @("a","b","c")
$output = "$($arr.length)"
Write-Output $output