1
00:00:00,540 --> 00:00:02,840
Okay, so now that we have jobs created,

2
00:00:02,840 --> 00:00:05,530
how do we remove the background jobs?

3
00:00:05,530 --> 00:00:09,360
Well, the command Remove‑Job is designed

4
00:00:09,360 --> 00:00:11,600
to delete a PowerShell background job.

5
00:00:11,600 --> 00:00:14,230
Nothing else. It just deletes a job. Now

6
00:00:14,230 --> 00:00:16,770
we can pass various parameters to it, but

7
00:00:16,770 --> 00:00:20,950
it will remove the job. Now we can remove

8
00:00:20,950 --> 00:00:23,800
background jobs by the name. So if we've

9
00:00:23,800 --> 00:00:26,280
defined the name or we use the name that

10
00:00:26,280 --> 00:00:29,560
was initially associated to it, we can

11
00:00:29,560 --> 00:00:32,410
delete every single job. We can delete the

12
00:00:32,410 --> 00:00:34,560
job by the state of the job. So, for

13
00:00:34,560 --> 00:00:36,520
example, get me all the jobs that have

14
00:00:36,520 --> 00:00:39,440
failed, and then remove them. We can also

15
00:00:39,440 --> 00:00:42,040
delete any jobs that were initially

16
00:00:42,040 --> 00:00:45,360
created by using the Invoke‑Command. And

17
00:00:45,360 --> 00:00:47,240
then we can also delete by the instance

18
00:00:47,240 --> 00:00:49,110
ID, which is actually the most common.

19
00:00:49,110 --> 00:00:52,130
You'll basically run Get‑Job and then get

20
00:00:52,130 --> 00:00:54,390
the ID and then just Remove‑Job and get

21
00:00:54,390 --> 00:00:57,810
rid of it. Now to remove jobs, we can

22
00:00:57,810 --> 00:00:59,880
remove it by name. So, first off, we

23
00:00:59,880 --> 00:01:02,220
populate a variable called Job, and we'll

24
00:01:02,220 --> 00:01:05,300
say, Get‑Job and then pass the ‑Name. And

25
00:01:05,300 --> 00:01:07,630
then we can use standard PowerShell syntax

26
00:01:07,630 --> 00:01:11,370
to basically say $job | Remove‑Job, and

27
00:01:11,370 --> 00:01:13,950
that will remove that job for us. If I

28
00:01:13,950 --> 00:01:16,700
want to remove it by the instance ID, then

29
00:01:16,700 --> 00:01:19,090
what I can do here is if we've started the

30
00:01:19,090 --> 00:01:21,430
job, so I have one here called Start‑Job

31
00:01:21,430 --> 00:01:24,110
and its Get‑Process of PowerShell, I can

32
00:01:24,110 --> 00:01:27,140
then retrieve the list of the properties,

33
00:01:27,140 --> 00:01:29,660
copy the instance ID, and then just say

34
00:01:29,660 --> 00:01:32,360
Remove‑Job ‑InstanceId and pass the GUID

35
00:01:32,360 --> 00:01:35,430
identifier. If I wanted to remove

36
00:01:35,430 --> 00:01:37,920
Invoke‑Command ones, this one's a bit more

37
00:01:37,920 --> 00:01:40,940
complicated because we have to kind of use

38
00:01:40,940 --> 00:01:43,890
different ways of retrieving those types

39
00:01:43,890 --> 00:01:46,010
of commands. So, for example, you'll see

40
00:01:46,010 --> 00:01:47,940
here we're doing a new PowerShell session

41
00:01:47,940 --> 00:01:50,330
on a specific computer, so this would be

42
00:01:50,330 --> 00:01:53,010
representative of a remote connection. I'm

43
00:01:53,010 --> 00:01:55,730
then going to say invoke a command on that

44
00:01:55,730 --> 00:02:00,050
specific session and say Start‑Job, go and

45
00:02:00,050 --> 00:02:03,060
get the process, and then call it

46
00:02:03,060 --> 00:02:05,600
something called Job. Then I can execute

47
00:02:05,600 --> 00:02:07,860
another Invoke‑Command in that same

48
00:02:07,860 --> 00:02:10,290
session. But this time, instead of calling

49
00:02:10,290 --> 00:02:13,540
Get‑Process, I'm going to say Remove‑Job.

50
00:02:13,540 --> 00:02:15,300
So remember how this works. The idea of

51
00:02:15,300 --> 00:02:18,760
Invoke‑Command is to initiate something on

52
00:02:18,760 --> 00:02:21,310
a remote device. So that means I'm

53
00:02:21,310 --> 00:02:23,340
creating a new PowerShell session on a

54
00:02:23,340 --> 00:02:26,090
remote server, executing a command, and

55
00:02:26,090 --> 00:02:31,000
then removing that command afterwards by using another Invoke‑Command.

