How can I schedule a task to run immediately?

Hi All,
How can I schedule a task to run immediately?

I have a script that sets a scheduled task on a remote computer, but Im having trouble having it just run once and as soon as its scheduled. AS it is now I can set whatever time I like, this is Daily at 8PM
(I know wsus can do this, but I am trying with PS)
Here is my script:

#Windowsupdates.ps1
cls
$remotecomputer = Read-Host -Prompt 'Please enter computer name to Install Windows Updates on'
$Credential = Get-Credential
$SessionRemote = New-Pssession $remotecomputer
$Path = "\\$remotecomputer\c$\Temp\"
Copy-Item -path 'C:\Temp\ITScript\WindowsUpdates\InstallAllWindowsUpdatesAndLog.ps1' -Destination 'c:\Temp' -ToSession $SessionRemote
Invoke-Command -ComputerName $remotecomputer -Credential $Credential -ScriptBlock {
	$Action = New-ScheduledTaskAction -Execute "C:\Program Files\PowerShell\7\pwsh.exe" `
		-Argument "C:\Temp\InstallAllWindowsUpdatesAndLog.ps1" # Specify what program to run and with its parameters
	$Trigger = New-ScheduledTaskTrigger -Daily -At '8PM'  # Specify the trigger settings
	$User = "NT AUTHORITY\SYSTEM" # Specify the account to run the script
	Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "WindowsUpdates8PM" -User $User -RunLevel Highest -Force # Specify the name of the task
}

Thanks,
Matt

I almost have a solution

I need the time output like 08:00PM

This gives me:

$time = get-date -Format "hh:mm"
	$Trigger = New-ScheduledTaskTrigger -Once -At "$time"  # Specify the trigger settings

08:00

as output. How can I add the AM/PM there?

I ran the script and it scheduled the task for 8:00AM instead of 8:00PM. I think if I specify the AM/PM in the variable it might work.

Thanks,

Matt

This does the trick:

$time = get-date -UFormat %r

output:
08:00:00 PM

According to the documentation …

… you don’t need a format at all. The parameter -At takes a value of type [DateTime]

Matt,

You can wrap srcipblock in a “Register-ScheduledJob” like below:

Invoke-Command -ComputerName $remotecomputer -Credential $Credential -ScriptBlock {
Register-ScheduledJob -Name Install -RunNow -ScriptBlock {
	$Action = New-ScheduledTaskAction -Execute "C:\Program Files\PowerShell\7\pwsh.exe" `
		-Argument "C:\Temp\InstallAllWindowsUpdatesAndLog.ps1" # Specify what program to run and with its parameters
	$Trigger = New-ScheduledTaskTrigger -Daily -At '8PM'  # Specify the trigger settings
	$User = "NT AUTHORITY\SYSTEM" # Specify the account to run the script
    Unregister-ScheduledJob -Name Install -Confirm:$false
  }
}

This will run immediately if this is what you are looking for.