Scheduled task syntax

Welcome Forums General PowerShell Q&A Scheduled task syntax

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 10 months ago.

  • Author
    Posts
  • #60696

    Participant
    Points: 0
    Rank: Member

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

  • #60699

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

  • #60700

    Participant
    Points: 0
    Rank: Member

    Ah of course. Many thanks

The topic ‘Scheduled task syntax’ is closed to new replies.