So, let's talk about multitasking within
PowerShell. While PowerShell is a
single‑threaded application, it cannot
start running another command in the same
console until the first one finishes, and
you need to make the decision up front to
move to a long‑running command instead of
just trying to inject other things into
the pipeline. Now it's important to
understand the difference between
asynchronous and synchronous. For
asynchronous and synchronous, first off,
PowerShell runs normal commands
synchronously, meaning that when you hit
Return, you then have to wait for that
command to complete. If you move the job
into a background job, it allows it to run
asynchronously, meaning that you can
continue to use the shell for other tasks
while that other command completes. When
we think of synchronous commands, it
allows you to respond to specific input
requests. It will produce error messages
when something goes wrong that you can
visibly see. It will also display results
as soon as they become available, so if
you're querying something, you'll get
results back. And then, of course, it can
prompt you for any missing information
that was required for that command. On the
flip side of this, when using asynchronous
commands, you are not allowed to see any
of the input requests. It can produce
error messages, but you are often unable
to see them. It will display results at
the end of the running task. And there's
no prompt for any missing information, the
command itself will just fail. So what are
the options for multitasking or
multithreading? Well, the first one here
is background job, which is what we're
going to focus on. The second is a
PowerShell runspace. Now we're not going
to talk about those too much in this one, we'll just focus on background jobs.