Scheduled task syntax

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.’

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.

Daniel

Ah of course. Many thanks