Okay, so now that we have jobs created,
how do we remove the background jobs?
Well, the command Remove‑Job is designed
to delete a PowerShell background job.
Nothing else. It just deletes a job. Now
we can pass various parameters to it, but
it will remove the job. Now we can remove
background jobs by the name. So if we've
defined the name or we use the name that
was initially associated to it, we can
delete every single job. We can delete the
job by the state of the job. So, for
example, get me all the jobs that have
failed, and then remove them. We can also
delete any jobs that were initially
created by using the Invoke‑Command. And
then we can also delete by the instance
ID, which is actually the most common.
You'll basically run Get‑Job and then get
the ID and then just Remove‑Job and get
rid of it. Now to remove jobs, we can
remove it by name. So, first off, we
populate a variable called Job, and we'll
say, Get‑Job and then pass the ‑Name. And
then we can use standard PowerShell syntax
to basically say $job | Remove‑Job, and
that will remove that job for us. If I
want to remove it by the instance ID, then
what I can do here is if we've started the
job, so I have one here called Start‑Job
and its Get‑Process of PowerShell, I can
then retrieve the list of the properties,
copy the instance ID, and then just say
Remove‑Job ‑InstanceId and pass the GUID
identifier. If I wanted to remove
Invoke‑Command ones, this one's a bit more
complicated because we have to kind of use
different ways of retrieving those types
of commands. So, for example, you'll see
here we're doing a new PowerShell session
on a specific computer, so this would be
representative of a remote connection. I'm
then going to say invoke a command on that
specific session and say Start‑Job, go and
get the process, and then call it
something called Job. Then I can execute
another Invoke‑Command in that same
session. But this time, instead of calling
Get‑Process, I'm going to say Remove‑Job.
So remember how this works. The idea of
Invoke‑Command is to initiate something on
a remote device. So that means I'm
creating a new PowerShell session on a
remote server, executing a command, and
then removing that command afterwards by using another Invoke‑Command.