1
00:00:01,240 --> 00:00:01,820
Finally,

2
00:00:01,820 --> 00:00:04,700
let me show you some other ways that you can format

3
00:00:04,700 --> 00:00:07,220
strings and information in PowerShell,

4
00:00:07,220 --> 00:00:11,940
as well as creating custom output that does exactly what you need.

5
00:00:11,940 --> 00:00:15,670
Let's get the bits service, my favorite service to work with.

6
00:00:15,670 --> 00:00:19,180
Now we looked at this earlier in the course where if I want to build a

7
00:00:19,180 --> 00:00:23,810
string that says the bits service is running or is stopped,

8
00:00:23,810 --> 00:00:28,940
we looked at ways of using sub expressions instead of using concatenation.

9
00:00:28,940 --> 00:00:32,610
I'm going to show you another way, and that is using the ‑f operator,

10
00:00:32,610 --> 00:00:34,590
or the format operator.

11
00:00:34,590 --> 00:00:35,520
In the course downloads,

12
00:00:35,520 --> 00:00:38,210
I have some links to some Microsoft documentation that will

13
00:00:38,210 --> 00:00:41,940
explain in a bit more detail what I am doing.

14
00:00:41,940 --> 00:00:47,540
So I'm going to create a string, and the string has placeholders,

15
00:00:47,540 --> 00:00:50,540
0 and 1 in curly braces.

16
00:00:50,540 --> 00:00:54,250
So the string is on the left side of the ‑f operator.

17
00:00:54,250 --> 00:00:59,780
On the right side of the operator, I have a comma‑separated list of values.

18
00:00:59,780 --> 00:01:03,710
So I'm getting the name property of the service

19
00:01:03,710 --> 00:01:06,540
object and then the status property.

20
00:01:06,540 --> 00:01:11,400
Those go into the corresponding placeholders 0 and 1.

21
00:01:11,400 --> 00:01:13,980
They don't have to be, by the way, in order.

22
00:01:13,980 --> 00:01:16,470
If I added something later on, and it was 2,

23
00:01:16,470 --> 00:01:20,140
like that could go in front of the 0.

24
00:01:20,140 --> 00:01:20,840
Typically though,

25
00:01:20,840 --> 00:01:23,030
try to keep them in order as you're getting used to this

26
00:01:23,030 --> 00:01:25,540
because it'll be easier to keep things straight.

27
00:01:25,540 --> 00:01:28,690
Now I get the string that says bits is stopped.

28
00:01:28,690 --> 00:01:33,280
The name and status plugged into the placeholders 0 and 1,

29
00:01:33,280 --> 00:01:36,140
and I get the string, bits is stopped.

30
00:01:36,140 --> 00:01:39,700
This may be an easier way to build strings for messages

31
00:01:39,700 --> 00:01:41,630
that you want to display to a user.

32
00:01:41,630 --> 00:01:44,120
This may be an easier way to build strings that you might

33
00:01:44,120 --> 00:01:47,140
want to use in your own PowerShell work.

34
00:01:47,140 --> 00:01:52,840
Now we can also use this ‑ f operator and format things like dates and numbers.

35
00:01:52,840 --> 00:01:55,060
So let's format a number.

36
00:01:55,060 --> 00:01:57,860
There is an f qualifier that we can use.

37
00:01:57,860 --> 00:02:00,250
So I'm going to take the number that's on the right side,

38
00:02:00,250 --> 00:02:07,990
1234.56 and so on, that is going to be plugged into the 0 placeholder,

39
00:02:07,990 --> 00:02:12,260
and then the f qualifier says treat this as a fixed number.

40
00:02:12,260 --> 00:02:16,840
By default, it truncates it to two decimal points.

41
00:02:16,840 --> 00:02:19,160
Now it's very important that you recognize here

42
00:02:19,160 --> 00:02:21,340
that when you use the ‑f operator,

43
00:02:21,340 --> 00:02:24,490
PowerShell is writing a string to the pipeline.

44
00:02:24,490 --> 00:02:29,240
So even though this looks like a number, this is actually a string.

45
00:02:29,240 --> 00:02:31,910
So if you were to use this code in a, say,

46
00:02:31,910 --> 00:02:35,600
select object expression, and try to sort on the value,

47
00:02:35,600 --> 00:02:39,740
it's going to sort it as a string, not as a number.

48
00:02:39,740 --> 00:02:41,540
Here's another example,

49
00:02:41,540 --> 00:02:46,100
I'm going to format the value as a fixed number with no decimal points,

50
00:02:46,100 --> 00:02:49,780
again, this is going to be a string, or I can say,

51
00:02:49,780 --> 00:02:51,630
you know what, I need three decimal points,

52
00:02:51,630 --> 00:02:54,540
whatever you need.

53
00:02:54,540 --> 00:02:59,440
Use the n operator, and these qualifiers, by the way, are case sensitive.

54
00:02:59,440 --> 00:03:03,840
Using the n qualifier has a nice effect of giving you comma output.

55
00:03:03,840 --> 00:03:11,600
A p will be a percentage, and this includes the percent symbol.

56
00:03:11,600 --> 00:03:13,590
Again, this is kind of a nice thing to have,

57
00:03:13,590 --> 00:03:16,690
makes your output maybe a little bit easier to read.

58
00:03:16,690 --> 00:03:19,820
If you need to specify the number of decimal points,

59
00:03:19,820 --> 00:03:22,510
you can do p and then the number of decimal points.

60
00:03:22,510 --> 00:03:27,440
So I'm going to do this to 4, and there is the output.

61
00:03:27,440 --> 00:03:32,640
There is also the ability to display or format a value in a currency,

62
00:03:32,640 --> 00:03:34,760
and the currency will default to whatever your

63
00:03:34,760 --> 00:03:37,150
regional or international settings are.

64
00:03:37,150 --> 00:03:42,740
So in my case, it's the dollar sign, and I get $23.56.

65
00:03:42,740 --> 00:03:44,940
These are kind of fun things to play with.

66
00:03:44,940 --> 00:03:49,040
We also have similar things that we can do with datetime objects.

67
00:03:49,040 --> 00:03:52,130
So let's create a datetime object $d,

68
00:03:52,130 --> 00:03:57,140
and I'm going to format this using the d qualifier.

69
00:03:57,140 --> 00:03:59,200
Now these qualifiers that you're seeing here,

70
00:03:59,200 --> 00:03:59,820
the lowercase,

71
00:03:59,820 --> 00:04:02,850
these are the same values that you can use in the

72
00:04:02,850 --> 00:04:06,140
dash format parameter for Get‑Date.

73
00:04:06,140 --> 00:04:10,040
So you can see this gives me just a date string.

74
00:04:10,040 --> 00:04:15,760
Capital D, remember I said they're case sensitive, gives me a long date string.

75
00:04:15,760 --> 00:04:18,570
There are methods on the datetime object we've looked at

76
00:04:18,570 --> 00:04:21,010
that will also give you the same result.

77
00:04:21,010 --> 00:04:24,930
Whether you use the method or the ‑f operator really depends

78
00:04:24,930 --> 00:04:29,640
upon the context of what you are trying to do.

79
00:04:29,640 --> 00:04:33,570
A lowercase g gives me a datetime string.

80
00:04:33,570 --> 00:04:42,740
Again, these are all strings, and f gives it to me with a little more detail.

81
00:04:42,740 --> 00:04:48,540
Lowercase u gives me that UTC string,

82
00:04:48,540 --> 00:04:51,540
and I can also format with a case‑sensitive string like this,

83
00:04:51,540 --> 00:04:54,960
year, day, month, the month is capital M,

84
00:04:54,960 --> 00:04:57,440
minutes would be lowercase m.

85
00:04:57,440 --> 00:05:00,840
So I'm trying to build a string, say, for a file name or a directory name,

86
00:05:00,840 --> 00:05:10,000
this is one way that I could do that, I could save this a variable, and then use that variable in the command to create the item.

