1
00:00:01,040 --> 00:00:05,140
Now let's look at operators that were introduced in PowerShell 7.

2
00:00:05,140 --> 00:00:07,720
First up is the chain operator.

3
00:00:07,720 --> 00:00:11,940
The chain operator is the two ampersands together.

4
00:00:11,940 --> 00:00:16,860
The way this works is if the left side expression is successful,

5
00:00:16,860 --> 00:00:19,850
such as get‑process ‑id $pid,

6
00:00:19,850 --> 00:00:25,340
then execute whatever is on the right side of the chain operator.

7
00:00:25,340 --> 00:00:29,090
So get‑process will run, I've got the process ID,

8
00:00:29,090 --> 00:00:32,270
and then I'm also going to display the number 100.

9
00:00:32,270 --> 00:00:34,020
Again, this is somewhat artificial,

10
00:00:34,020 --> 00:00:39,240
but I want you to be able to see what the operator is doing.

11
00:00:39,240 --> 00:00:44,340
And there is the result that you might expect to see in a PowerShell 7 session.

12
00:00:44,340 --> 00:00:47,280
There was the process, the process completed without error,

13
00:00:47,280 --> 00:00:50,180
and so the right side of the chain operator worked,

14
00:00:50,180 --> 00:00:53,040
and I get the value of 100.

15
00:00:53,040 --> 00:00:55,310
If the first command fails,

16
00:00:55,310 --> 00:00:58,970
then the second expression on the right side of the chain operator never runs.

17
00:00:58,970 --> 00:01:01,710
So I try to run get‑process for a nonexistent ID.

18
00:01:01,710 --> 00:01:02,760
I get an error.

19
00:01:02,760 --> 00:01:04,920
That command does not complete successfully,

20
00:01:04,920 --> 00:01:08,430
and so the second part of that chain does not run,

21
00:01:08,430 --> 00:01:11,640
and I never exceed the value of 100.

22
00:01:11,640 --> 00:01:16,040
The other chain operator is the two vertical bars put together.

23
00:01:16,040 --> 00:01:17,230
In this case,

24
00:01:17,230 --> 00:01:23,340
if the left side expression fails then run the right side expression.

25
00:01:23,340 --> 00:01:26,320
So if I do get‑process for some nonexistent process,

26
00:01:26,320 --> 00:01:29,530
because that fails then I get, in this case,

27
00:01:29,530 --> 00:01:31,540
the number 100.

28
00:01:31,540 --> 00:01:35,500
Here's an example of how you might use this in PowerShell.

29
00:01:35,500 --> 00:01:38,830
If the first test succeeds, then I want to do something.

30
00:01:38,830 --> 00:01:43,090
So, if I can do a test‑wsman on a computer,

31
00:01:43,090 --> 00:01:45,930
in this case my local computer name, then,

32
00:01:45,930 --> 00:01:47,470
that's the ampersand ampersand,

33
00:01:47,470 --> 00:01:49,940
then I'm going to run Get‑CimInstance and get the

34
00:01:49,940 --> 00:01:52,570
win32_bios class on that computer.

35
00:01:52,570 --> 00:01:56,710
So it's a nice example of using the chain operator.

36
00:01:56,710 --> 00:02:00,340
If the left side of the expression works,

37
00:02:00,340 --> 00:02:02,190
or runs without error I should say,

38
00:02:02,190 --> 00:02:05,890
then run whatever is on the right side of the chain operator.

39
00:02:05,890 --> 00:02:08,850
Now if you wanted, for example though, to get rid of say,

40
00:02:08,850 --> 00:02:12,370
I don't want to see that first command, you can pipe,

41
00:02:12,370 --> 00:02:17,940
this is going to pipe the test‑wsman to Out‑Null to suppress that test result.

42
00:02:17,940 --> 00:02:22,490
So all I get then is the result of the Get‑CimInstance command

43
00:02:22,490 --> 00:02:25,360
on the right side of the chain operator,

44
00:02:25,360 --> 00:02:27,140
the double ampersand.

45
00:02:27,140 --> 00:02:30,340
So here's an example where this is not going to work.

46
00:02:30,340 --> 00:02:32,850
So I'm going to try to test the $computer = "foo".

47
00:02:32,850 --> 00:02:34,520
That obviously doesn't exist.

48
00:02:34,520 --> 00:02:37,490
So if that works, then run Get‑CimInstance.

49
00:02:37,490 --> 00:02:41,710
And then if that doesn't work, then write a warning.

50
00:02:41,710 --> 00:02:46,140
So, I'm kind of combining both chain operators here.

51
00:02:46,140 --> 00:02:52,010
And so I get my failure, so I never run Get‑CimInstance,

52
00:02:52,010 --> 00:02:54,490
but because that first expression failed,

53
00:02:54,490 --> 00:02:56,240
then I get the warning.

54
00:02:56,240 --> 00:02:59,540
You'll, again, want to try these things all out for yourself.

55
00:02:59,540 --> 00:03:02,290
You could also do a lot of the same things in PowerShell

56
00:03:02,290 --> 00:03:10,000
scripting with an if/else statement. There is a help topic that you should take a look at.

