Today, I had the need to replicate the bash command at in a PowerShell script that I was writing for an AWS SSM Document.
In bash, the command looks like this:
echo 'echo hello world' | at now + 1 minute
You won’t see the output of this echo in your terminal, since it’s being run in another process, but sure enough, the echo hello world command will run a minute later.
PowerShell doesn’t have this same functionality and so I have found the following way of reproducing it. If you have a better way, please do let me know in the comments.
Background Jobs
First, I create the script I want to run and I save it into the $myjob variable.
$myjob = {Start-Sleep 5; Write-Host "hello world"}
I can then run the Job as a Background Job.
$refjob = Start-Job -ScriptBlock $myjob
I am then able to go about executing other commands and my process is not blocked.
I can check on the progress of the job by looking at the state:
$refjob.State
I can also awkwardly check on the job (whilst blocking the process) using the following syntax (If you have any ideas on how I can do this more cleanly, please do let me know):
do { Start-Sleep -Milliseconds 100 } until ($refjob.State -eq "Completed")
Finally, to clear things up, I pipe the Job Reference to the Remove-Job Cmdlet.
$refjob | Remove-Job