1
00:00:01,140 --> 00:00:04,690
The next Out cmdlet I'd like to show you is Out‑String.

2
00:00:04,690 --> 00:00:06,640
Let's take a look.

3
00:00:06,640 --> 00:00:10,340
Okay, first off, we'll explore the help for Out‑String,

4
00:00:10,340 --> 00:00:14,240
and this cmdlet will output the object as a string.

5
00:00:14,240 --> 00:00:17,300
There's only a couple of parameters available with Out‑String,

6
00:00:17,300 --> 00:00:19,640
as you can see in the syntax block.

7
00:00:19,640 --> 00:00:24,390
And as noted in the description, Out‑String converts objects into strings,

8
00:00:24,390 --> 00:00:25,470
and, by default,

9
00:00:25,470 --> 00:00:30,640
it accumulates the strings and returns them as one single string.

10
00:00:30,640 --> 00:00:33,200
There's a parameter called Stream, which you can use,

11
00:00:33,200 --> 00:00:37,090
which will tell Out‑String to just process each object at a time.

12
00:00:37,090 --> 00:00:42,040
So that ends up returning an array of strings rather than one single string.

13
00:00:42,040 --> 00:00:45,790
I want to pause here just briefly and give you a reminder that

14
00:00:45,790 --> 00:00:48,910
PowerShell is primarily an object‑based system.

15
00:00:48,910 --> 00:00:52,510
So while you will end up needing to work with strings in one form or

16
00:00:52,510 --> 00:00:55,500
another at some point in your PowerShell journey,

17
00:00:55,500 --> 00:00:57,770
I definitely don't recommend converting objects to

18
00:00:57,770 --> 00:01:00,640
strings unless you really need to.

19
00:01:00,640 --> 00:01:04,070
It might feel normal for those that are coming from a Linux background to

20
00:01:04,070 --> 00:01:06,910
want to work with strings and raw text in the output.

21
00:01:06,910 --> 00:01:11,010
But you'll make life much easier if you can do your job working with

22
00:01:11,010 --> 00:01:14,840
PowerShell objects rather than text and strings.

23
00:01:14,840 --> 00:01:16,750
Let's run this thing to see what happens.

24
00:01:16,750 --> 00:01:22,140
So we'll grab Get‑Process and pipe that across to Out‑String.

25
00:01:22,140 --> 00:01:23,110
Interestingly,

26
00:01:23,110 --> 00:01:25,640
this output looks pretty similar to what we've just seen

27
00:01:25,640 --> 00:01:27,840
when running Get‑Process by itself.

28
00:01:27,840 --> 00:01:31,080
We've got a table structure on the screen with processes listed,

29
00:01:31,080 --> 00:01:34,210
but what you don't notice here immediately is that this is a

30
00:01:34,210 --> 00:01:37,230
single object that is just a string of text.

31
00:01:37,230 --> 00:01:40,040
In some of the other clips in this course,

32
00:01:40,040 --> 00:01:42,430
we've piped Get‑Process to Get‑Member,

33
00:01:42,430 --> 00:01:46,840
which has shown us the properties of the process object that we can work with.

34
00:01:46,840 --> 00:01:49,860
If I rerun the command to output this to a string,

35
00:01:49,860 --> 00:01:51,280
we'll pipe that to Get‑Member,

36
00:01:51,280 --> 00:01:55,240
and I'll tack on the more command here so it doesn't run off the screen.

37
00:01:55,240 --> 00:01:57,560
Notice here at the top that the type of object that is

38
00:01:57,560 --> 00:02:01,240
currently in the pipeline is System.String.

39
00:02:01,240 --> 00:02:05,600
That object has absolutely nothing to do with processes and process objects

40
00:02:05,600 --> 00:02:09,740
because we converted it to a string using Out‑String.

41
00:02:09,740 --> 00:02:13,700
All of the items that we're seeing here as I page through are methods of

42
00:02:13,700 --> 00:02:17,040
things that we can do with string objects in PowerShell.

43
00:02:17,040 --> 00:02:20,610
It's important that you understand that after piping to Out‑String,

44
00:02:20,610 --> 00:02:23,080
you can now no longer work with the original

45
00:02:23,080 --> 00:02:25,940
PowerShell objects and their properties.

46
00:02:25,940 --> 00:02:30,110
For example, I'll create a new variable named $process.

47
00:02:30,110 --> 00:02:33,540
And in that, we'll store the results of Get‑Process.

48
00:02:33,540 --> 00:02:37,340
These are the raw PowerShell objects of the process type.

49
00:02:37,340 --> 00:02:39,510
Let's check out the count property to see how many

50
00:02:39,510 --> 00:02:43,840
objects we've got in that variable, and we've currently got 80.

51
00:02:43,840 --> 00:02:47,780
I can get the first process stored in that collection of 80 by running

52
00:02:47,780 --> 00:02:51,020
$process and telling it I want the first object,

53
00:02:51,020 --> 00:02:54,330
which we can reference by using an opening square bracket,

54
00:02:54,330 --> 00:02:59,240
0, to say I want the first object, and a closing square bracket.

55
00:02:59,240 --> 00:03:02,090
PowerShell returns the first object in the variable.

56
00:03:02,090 --> 00:03:02,800
Sweet.

57
00:03:02,800 --> 00:03:07,070
I can choose to get the second object in the variable by doing the same thing,

58
00:03:07,070 --> 00:03:08,940
but specifying a 1.

59
00:03:08,940 --> 00:03:11,830
I can also pipe the variable to the select object

60
00:03:11,830 --> 00:03:18,140
cmdlet and ask for the first object, or I can even ask for the first five.

61
00:03:18,140 --> 00:03:20,340
If I pipe this across to Get‑Member,

62
00:03:20,340 --> 00:03:24,150
notice at the top that the type is System.Diagnostics.Process.

63
00:03:24,150 --> 00:03:26,100
And as I page through here,

64
00:03:26,100 --> 00:03:30,640
these are all of the underlying properties related to process objects.

65
00:03:30,640 --> 00:03:33,330
The point I'm trying to drive home with this example is that

66
00:03:33,330 --> 00:03:36,640
we're still working with objects in the console.

67
00:03:36,640 --> 00:03:39,940
Let's do some similar experiments when working with a string.

68
00:03:39,940 --> 00:03:42,860
So I'll use the same $process variable.

69
00:03:42,860 --> 00:03:47,540
And in that, we'll do Get‑Process and pipe it across to Out‑String.

70
00:03:47,540 --> 00:03:51,230
If we check the count of objects in the $process variable,

71
00:03:51,230 --> 00:03:53,390
so last time when we were dealing with objects,

72
00:03:53,390 --> 00:03:54,590
we had 80.

73
00:03:54,590 --> 00:03:56,610
But now that I've converted it to a string,

74
00:03:56,610 --> 00:03:59,340
there's only one object in the variable.

75
00:03:59,340 --> 00:04:05,440
If I run $process, that single object of a string gets output to the screen.

76
00:04:05,440 --> 00:04:10,740
Let's run $process and tell PowerShell that we want just the first object.

77
00:04:10,740 --> 00:04:14,380
So when we did this before, we got the first process in the variable.

78
00:04:14,380 --> 00:04:16,110
But now that we're dealing with a string,

79
00:04:16,110 --> 00:04:20,440
I don't have access to the objects in the same way that I did before.

80
00:04:20,440 --> 00:04:24,010
If I pipe it to Select‑Object and ask for the first object,

81
00:04:24,010 --> 00:04:27,180
well, we've only got one, which is that full string.

82
00:04:27,180 --> 00:04:30,750
So PowerShell returns the entire string to our console.

83
00:04:30,750 --> 00:04:34,550
To finish up here, I'd like to show you the Stream parameter,

84
00:04:34,550 --> 00:04:38,620
which causes each block of text separated by a line break

85
00:04:38,620 --> 00:04:41,240
to be treated as a separate stream.

86
00:04:41,240 --> 00:04:45,020
Instead of storing the entire result as a single string object,

87
00:04:45,020 --> 00:04:48,050
it will store each object coming across the pipeline in

88
00:04:48,050 --> 00:04:50,840
its own string inside of an array.

89
00:04:50,840 --> 00:04:54,940
So same command as before, but let's pop on the Stream parameter.

90
00:04:54,940 --> 00:04:59,840
This time if we check the count, we've got 89 objects in total.

91
00:04:59,840 --> 00:05:03,440
If I get the first object, it's actually a blank line.

92
00:05:03,440 --> 00:05:05,050
But if I get the second object,

93
00:05:05,050 --> 00:05:07,230
this time I can see the names of the headings for the

94
00:05:07,230 --> 00:05:10,040
table that Get‑Process produces.

95
00:05:10,040 --> 00:05:11,970
And if we get the third object,

96
00:05:11,970 --> 00:05:13,970
it's a line of the dashes that are normally

97
00:05:13,970 --> 00:05:16,320
underneath the table heading properties.

98
00:05:16,320 --> 00:05:18,260
And if we go for the fourth object,

99
00:05:18,260 --> 00:05:21,110
we get back the string relating to the first service.

100
00:05:21,110 --> 00:05:23,180
So when using the Stream parameter,

101
00:05:23,180 --> 00:05:27,230
each object that came down the pipeline is stored as its own string.

102
00:05:27,230 --> 00:05:30,240
And with that said, if I pipe that across to Get‑Member,

103
00:05:30,240 --> 00:05:37,000
we're still working with a string object here, not the object that was the output of the original Get‑Process command.

