Convert PS1 script to one liner - new scheduled task

Pavel yannara Mirochnitchenko 13,336 Reputation points MVP
2023-08-30T10:49:33.23+00:00

Is there a way to convert this script only to 1 line. When I try to open the variables, I get the error.

# Create a PowerShell script that contains the Restart-Computer cmdlet

$script = "Restart-Computer -Force"

$scriptFile = "C:\restart.ps1"

$script | Out-File $scriptFile

# Create an action that runs the PowerShell script

$action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-File $scriptFile"

# Create a trigger that specifies when the task should run

$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5)

# Create a task object that contains the action and trigger

$task = New-ScheduledTask -Action $action -Trigger $trigger

# Register the task in the Task Scheduler

*Register-ScheduledTask -TaskName "Restart" -InputObject $task
*
I tried: "Register-ScheduledTask -TaskName "LABS Restart after Autopilot" -Action New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "Restart-Computer -Force" -Trigger New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5)"

Error: "Register-ScheduledTask : Cannot process argument transformation on parameter 'Action'. Cannot convert value "New-ScheduledTaskAction" to type"

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2023-08-30T14:46:56.2866667+00:00

    How about this?

    $action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command Restart-Computer -Force"
    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5)
    $usr = New-ScheduledTaskPrincipal -GroupId SYSTEM
    $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $usr
    Register-ScheduledTask -TaskName "Restart"  -InputObject $task -Force 
    
    

    In one line.

    $action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command Restart-Computer -Force";$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5);$usr = New-ScheduledTaskPrincipal -GroupId SYSTEM;$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $usr;Register-ScheduledTask -TaskName "Restart"  -InputObject $task -Force 
    
    0 comments No comments

  2. Anonymous
    2023-08-31T05:30:01.89+00:00

    Hi,

    Please see if this works.

    Register-ScheduledTask -TaskName "LABS Restart after Autopilot" -Action $(New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "Restart-Computer -Force") -Trigger $(New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5))
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.