1
00:00:00,040 --> 00:00:04,620
So to get started, let's talk about converting and formatting data values.

2
00:00:04,620 --> 00:00:08,090
This is a common task that when building PowerShell

3
00:00:08,090 --> 00:00:09,430
scripts that you'll have to do,

4
00:00:09,430 --> 00:00:13,800
which will be converting a number to something else or a text value to

5
00:00:13,800 --> 00:00:16,750
something else or maybe going a little bit deeper into,

6
00:00:16,750 --> 00:00:19,640
for example, .NET typed objects.

7
00:00:19,640 --> 00:00:22,840
So first off, what type of variables are there?

8
00:00:22,840 --> 00:00:25,840
Well, there's what's referred to as loosely typed.

9
00:00:25,840 --> 00:00:31,690
These are when a value is assigned to effectively an undefined type of variable.

10
00:00:31,690 --> 00:00:35,210
Now as you've seen so far in the previous modules,

11
00:00:35,210 --> 00:00:39,320
we just create a variable called $variable and assign a value.

12
00:00:39,320 --> 00:00:43,230
That would be a loosely typed variable.

13
00:00:43,230 --> 00:00:47,240
The second option is a strongly typed variable.

14
00:00:47,240 --> 00:00:51,020
This is where you are assigning the type to that variable.

15
00:00:51,020 --> 00:00:53,900
So, for example, this variable will be a integer,

16
00:00:53,900 --> 00:01:01,450
or this will be a string, and the only type of data that it can contain is that.

17
00:01:01,450 --> 00:01:05,450
We can use variables to store data.

18
00:01:05,450 --> 00:01:10,140
We can store all types of values within PowerShell variables.

19
00:01:10,140 --> 00:01:15,640
A variable is just a unit of memory in which those values actually get stored.

20
00:01:15,640 --> 00:01:20,640
We then declare those variables by just using the dollar sign before the name.

21
00:01:20,640 --> 00:01:24,300
Variable names aren't necessarily case‑sensitive,

22
00:01:24,300 --> 00:01:27,620
and they can include spaces and special characters.

23
00:01:27,620 --> 00:01:31,170
But I will advise you to try and keep them without spaces

24
00:01:31,170 --> 00:01:35,290
and special characters and just create them with dollar and

25
00:01:35,290 --> 00:01:39,140
a name that makes sense to you.

26
00:01:39,140 --> 00:01:42,740
Now there are different variable types available to us.

27
00:01:42,740 --> 00:01:45,110
The first is user‑created variables.

28
00:01:45,110 --> 00:01:47,320
So we just go in and create them,

29
00:01:47,320 --> 00:01:50,420
and they're available to us in the context of the

30
00:01:50,420 --> 00:01:53,540
PowerShell script that we're executing.

31
00:01:53,540 --> 00:01:56,500
We then have what's referred to as automatic variables,

32
00:01:56,500 --> 00:01:58,440
and we'll talk a little bit more about this.

33
00:01:58,440 --> 00:02:01,170
And then, of course, we have preference variables.

34
00:02:01,170 --> 00:02:05,830
Preference variables are really around some of the configuration around the

35
00:02:05,830 --> 00:02:10,510
PowerShell script itself or the execution and things like that.

36
00:02:10,510 --> 00:02:13,350
So first off, we have user‑created variables.

37
00:02:13,350 --> 00:02:16,590
These are created and maintained by you as the user.

38
00:02:16,590 --> 00:02:19,920
The variables created within the command line exist

39
00:02:19,920 --> 00:02:23,180
only in the context of that window.

40
00:02:23,180 --> 00:02:28,940
So if I open up a PowerShell console and create $variable equals 10,

41
00:02:28,940 --> 00:02:31,150
then I can't close that window and reopen it and

42
00:02:31,150 --> 00:02:34,440
expect to find that same value there.

43
00:02:34,440 --> 00:02:37,720
The automatic variables store the state of PowerShell.

44
00:02:37,720 --> 00:02:42,650
Now PowerShell create these variables and changes their values as required,

45
00:02:42,650 --> 00:02:44,760
and you can execute this.

46
00:02:44,760 --> 00:02:49,740
So, for example, we can use a $ps, for example,

47
00:02:49,740 --> 00:02:53,510
and let's say environment or env, so psenvironment.

48
00:02:53,510 --> 00:02:57,240
It would be an automatic variable that we can access.

49
00:02:57,240 --> 00:02:59,470
Then, of course, we have preference variables.

50
00:02:59,470 --> 00:03:05,560
So they are variables that store user preferences for PowerShell.

51
00:03:05,560 --> 00:03:06,950
These variables, once again,

52
00:03:06,950 --> 00:03:11,800
get created by PowerShell and get populated with default values.

53
00:03:11,800 --> 00:03:15,140
However, they could be changed.

54
00:03:15,140 --> 00:03:17,440
So how do we create user variables?

55
00:03:17,440 --> 00:03:20,190
Well, it's as simple as doing this, dollar,

56
00:03:20,190 --> 00:03:23,650
the name, equals, and set it to a value.

57
00:03:23,650 --> 00:03:25,980
So you can see I have three variables here.

58
00:03:25,980 --> 00:03:29,830
One is set to a comma‑separated set of numbers.

59
00:03:29,830 --> 00:03:31,540
One is a string value.

60
00:03:31,540 --> 00:03:34,190
One is a string data value.

61
00:03:34,190 --> 00:03:35,520
Now don't get confused here.

62
00:03:35,520 --> 00:03:40,840
Just because it's in a date format does not make that variable date type.

63
00:03:40,840 --> 00:03:42,340
It's just a string.

64
00:03:42,340 --> 00:03:45,770
Now if I wanted to create a typed variable,

65
00:03:45,770 --> 00:03:47,860
I can do this a little bit differently.

66
00:03:47,860 --> 00:03:51,940
This is where we define what that variable can contain.

67
00:03:51,940 --> 00:03:56,920
So you'll see my variable 1 is now going to contain an integer.

68
00:03:56,920 --> 00:04:00,080
So in square brackets, I type Int,

69
00:04:00,080 --> 00:04:04,280
which is the strong type of what I wish to add in there.

70
00:04:04,280 --> 00:04:07,290
The same would go for the DateTime.

71
00:04:07,290 --> 00:04:11,250
Now there are common variable data types that we can

72
00:04:11,250 --> 00:04:14,400
utilize outside of int and datetime.

73
00:04:14,400 --> 00:04:19,040
We have boolean, so the yes/no value, a date time.

74
00:04:19,040 --> 00:04:23,240
We also have a PowerShell object, and we'll talk about those a little bit later.

75
00:04:23,240 --> 00:04:28,240
We then have a script block, which is obviously other executed PowerShell,

76
00:04:28,240 --> 00:04:31,000
a string value, which is obviously the most common,

77
00:04:31,000 --> 00:04:35,340
and then, of course, an array of objects.

78
00:04:35,340 --> 00:04:39,400
Now once we start grabbing data and populating it into variables,

79
00:04:39,400 --> 00:04:42,800
one of the most common things we have to do is to convert or

80
00:04:42,800 --> 00:04:46,640
what's referred to as cast those data values.

81
00:04:46,640 --> 00:04:50,900
Casting is about converting one object type to another so

82
00:04:50,900 --> 00:04:53,940
that we can utilize it into something else.

83
00:04:53,940 --> 00:04:56,820
Not all objects can be cast,

84
00:04:56,820 --> 00:04:59,750
so you can't just randomly say I want this type of

85
00:04:59,750 --> 00:05:02,540
value to be cast to something else.

86
00:05:02,540 --> 00:05:03,670
You have to be careful.

87
00:05:03,670 --> 00:05:06,440
So converting objects from one to another,

88
00:05:06,440 --> 00:05:10,940
often you would mitigate some of these issues by just defining the

89
00:05:10,940 --> 00:05:14,540
variables with the right types to begin with.

90
00:05:14,540 --> 00:05:18,040
So let's look at how we would cast variables and values.

91
00:05:18,040 --> 00:05:19,540
So I have two variables here.

92
00:05:19,540 --> 00:05:21,600
Variable 1 is the number 1.

93
00:05:21,600 --> 00:05:27,640
Variable 2 is clearly a date, 01/01/2021.

94
00:05:27,640 --> 00:05:31,620
If I want to convert a string variable to an integer,

95
00:05:31,620 --> 00:05:37,640
I'm going to prefix the variable with Int as the strong type.

96
00:05:37,640 --> 00:05:40,890
If I want to convert the string variable to a date,

97
00:05:40,890 --> 00:05:45,740
I need to put DateTime in front of that specific variable.

98
00:05:45,740 --> 00:05:49,610
If I want to convert a false value to an integer,

99
00:05:49,610 --> 00:05:53,710
so a false or a true, I can simply put int before it,

100
00:05:53,710 --> 00:05:58,840
and it will then give it its either 0 or 1 value.

101
00:05:58,840 --> 00:06:05,170
When a value is cast to a particular datatype, it's a one‑time change.

102
00:06:05,170 --> 00:06:07,380
So it doesn't have to happen all the time.

103
00:06:07,380 --> 00:06:11,940
It's just I converted it and changed it at that point.

104
00:06:11,940 --> 00:06:16,150
When a variable is cast to a particular datatype,

105
00:06:16,150 --> 00:06:20,340
it will stay that way unless it's updated again.

106
00:06:20,340 --> 00:06:22,140
So just think that through.

107
00:06:22,140 --> 00:06:27,450
If we go back one slide, when a value is cast to that

108
00:06:27,450 --> 00:06:30,040
datatype, it's a single transaction.

109
00:06:30,040 --> 00:06:33,260
When a variable is cast, it will stay that way.

110
00:06:33,260 --> 00:06:35,970
So if you define a variable as an integer,

111
00:06:35,970 --> 00:06:40,340
it will always stay an integer unless you update.

112
00:06:40,340 --> 00:06:44,740
Now what we do have is we have an operator called the ‑as operator.

113
00:06:44,740 --> 00:06:50,320
This will allow you to cast things to a specific thing. Now it also

114
00:06:50,320 --> 00:06:54,450
serves the purpose for being able to test a conversion. So you can use

115
00:06:54,450 --> 00:06:58,810
that as operator to say, well, if I convert this to that,

116
00:06:58,810 --> 00:07:01,340
will it work?

117
00:07:01,340 --> 00:07:06,480
You can also define the type after the variable by using the as command.

118
00:07:06,480 --> 00:07:07,680
And then, of course,

119
00:07:07,680 --> 00:07:12,200
the big thing here to worry about is that it can have unexpected results.

120
00:07:12,200 --> 00:07:15,980
It may not work the way that you're expecting because,

121
00:07:15,980 --> 00:07:17,070
as I mentioned before,

122
00:07:17,070 --> 00:07:21,340
you cannot cast everything into anything, and the as is such

123
00:07:21,340 --> 00:07:24,490
an easy way of doing it that it doesn't necessarily work the

124
00:07:24,490 --> 00:07:26,440
way that you're expecting.

125
00:07:26,440 --> 00:07:28,170
So what's the syntax look like?

126
00:07:28,170 --> 00:07:28,430
Well,

127
00:07:28,430 --> 00:07:31,840
let's go back and have the same two variables, variable1, string

128
00:07:31,840 --> 00:07:35,940
value number, variable2, date string value.

129
00:07:35,940 --> 00:07:39,710
If I want to cast it as an integer, I can simply say

130
00:07:39,710 --> 00:07:45,240
variable1 ‑as using the operator Int.

131
00:07:45,240 --> 00:07:49,490
If I want to do the date, I can do the same process and say as date.

132
00:07:49,490 --> 00:07:51,860
And if I wanted to do the same thing as I did before,

133
00:07:51,860 --> 00:07:57,150
so casting the false value to an integer, I can do false as an

134
00:07:57,150 --> 00:07:59,590
integer. So a couple of different ways of doing it.

135
00:07:59,590 --> 00:08:05,880
It really depends how you want to write the PowerShell script. I like to use

136
00:08:05,880 --> 00:08:12,840
the defined variables just because it kind of makes more sense that way that

137
00:08:12,840 --> 00:08:15,920
you control the input to get what you want.

138
00:08:15,920 --> 00:08:18,500
But if you're kind of ad hoc, on the fly,

139
00:08:18,500 --> 00:08:21,570
you may not want to have input or there's no input that

140
00:08:21,570 --> 00:08:24,510
comes in, this may be the output of something else, then

141
00:08:24,510 --> 00:08:26,240
you can use that as operator.

142
00:08:26,240 --> 00:08:29,640
Now we also have what's called the f operator.

143
00:08:29,640 --> 00:08:32,910
This is used to format a string expression.

144
00:08:32,910 --> 00:08:38,340
So instead of us casting objects, we can format the values into something else.

145
00:08:38,340 --> 00:08:45,380
This supports complex formatting. And you can begin the statement

146
00:08:45,380 --> 00:08:50,600
with the selected format, so what you would like that to be in. So

147
00:08:50,600 --> 00:08:52,820
let's look at how we format the data values.

148
00:08:52,820 --> 00:08:55,340
So what does this look like in PowerShell?

149
00:08:55,340 --> 00:08:57,160
So I have two variables here.

150
00:08:57,160 --> 00:09:02,220
You can see that one is a 123 point something,

151
00:09:02,220 --> 00:09:05,490
and then the other variable is just a standard number.

152
00:09:05,490 --> 00:09:07,740
It actually looks like a phone number.

153
00:09:07,740 --> 00:09:11,080
If I want to display only the three decimal places,

154
00:09:11,080 --> 00:09:13,420
you can see that we can use this kind of syntax.

155
00:09:13,420 --> 00:09:13,780
Now remember,

156
00:09:13,780 --> 00:09:17,200
we said we put this at the beginning of the variable. So you'll

157
00:09:17,200 --> 00:09:25,540
see that in quotes, in brackets, it will be 0: and then 0 being

158
00:09:25,540 --> 00:09:27,240
the actual value that's coming in.

159
00:09:27,240 --> 00:09:31,060
And then you've got n3, which will tell it that I only

160
00:09:31,060 --> 00:09:36,740
want to show the three decimal places, and then the ‑f is telling it to format.

161
00:09:36,740 --> 00:09:41,490
If I want to convert variable2 into a phone number with spaces,

162
00:09:41,490 --> 00:09:42,800
same principle again.

163
00:09:42,800 --> 00:09:44,560
You do this before the variable,

164
00:09:44,560 --> 00:09:48,870
and it will be squirrely brackets, 0 is the value coming in,

165
00:09:48,870 --> 00:09:52,160
and then the colon is the format on the right. And you can see, I'm

166
00:09:52,160 --> 00:10:00,150
doing 3‑3‑4. If I want to display the year only from the current

167
00:10:00,150 --> 00:10:04,310
date, I can do this and say 0: year, year,

168
00:10:04,310 --> 00:10:07,940
year year, which will then, I'm using not a variable.

169
00:10:07,940 --> 00:10:10,310
I'm using a command called Get‑Date,

170
00:10:10,310 --> 00:10:13,530
which will get me the current date of today, but only

171
00:10:13,530 --> 00:10:15,910
return back the year. Now of course,

172
00:10:15,910 --> 00:10:18,260
dates are the most common one that you would see where

173
00:10:18,260 --> 00:10:23,920
you would be formatting as in month, month ‑d, d for day, ‑year,

174
00:10:23,920 --> 00:10:24,760
year, year.

175
00:10:24,760 --> 00:10:30,000
So that's a common use case, but it works for numbers, as well as regular text values.

