Author Posts

December 22, 2016 at 10:57 am

I'm trying to set up a scheduled task that runs a powershell command to disable a domain account on a given day.

I don't understand why this works

$name = "test user"
Disable-ADAccount -Identity $name

And this does not?

$name = "test user"
powershell -NoProfile -WindowStyle Hidden -command {Disable-ADAccount -Identity $name}

Error: 'Cannot validate argument on parameter 'Identity'. The argument is null.'

December 22, 2016 at 11:29 am

You're using a script block {} which does not automatically expand outside variables. Try to replace the curly brackets with double-quotes and enclose the variable in single-quotes to ensure the space gets included.

$name = "test user"
powershell -NoProfile -WindowStyle Hidden -command "Disable-ADAccount -Identity '$name'"

I hope that helps.


December 22, 2016 at 11:46 am

Ah of course. Many thanks