1
00:00:00,140 --> 00:00:02,560
So let's go into the PowerShell console,

2
00:00:02,560 --> 00:00:05,470
and we'll look at how we can convert and format values.

3
00:00:05,470 --> 00:00:08,860
We'll look at base formatting of values first, then we'll

4
00:00:08,860 --> 00:00:12,510
cast values to a different type, and then we'll look at how

5
00:00:12,510 --> 00:00:15,320
we can cast using the AS operator.

6
00:00:15,320 --> 00:00:18,100
So we just talked about variables, so let's have a look

7
00:00:18,100 --> 00:00:20,630
at the practicalities of this. Well, first off,

8
00:00:20,630 --> 00:00:22,940
let's create a variable called variable1,

9
00:00:22,940 --> 00:00:26,600
and what we'll do is just add some specific numbers like so.

10
00:00:26,600 --> 00:00:29,890
If I want to view what's in the variable,

11
00:00:29,890 --> 00:00:31,940
then I can just obviously type variable.

12
00:00:31,940 --> 00:00:38,310
Now if I also declared a variable2, and maybe I just did a string

13
00:00:38,310 --> 00:00:43,770
value, so let's imagine it's a path to C:\Documents like so, I can

14
00:00:43,770 --> 00:00:47,360
then do variable2 and render that value.

15
00:00:47,360 --> 00:00:49,320
So nothing spectacular there.

16
00:00:49,320 --> 00:00:54,340
Those are just called regular basic variables that don't have any types.

17
00:00:54,340 --> 00:00:54,620
Now,

18
00:00:54,620 --> 00:00:57,410
if we were to change this and I wanted to make sure

19
00:00:57,410 --> 00:01:00,970
that the variable had a specific type,

20
00:01:00,970 --> 00:01:06,340
so if I say variable3 and I set that as an integer with a 0,

21
00:01:06,340 --> 00:01:11,940
then, of course, when I do variable3, it'll just return 10.

22
00:01:11,940 --> 00:01:16,840
Now what about if I change that and put the 10 in quotes like so?

23
00:01:16,840 --> 00:01:18,570
Is it going to return the same value?

24
00:01:18,570 --> 00:01:19,320
Yes, it does.

25
00:01:19,320 --> 00:01:21,170
It returns a 10.

26
00:01:21,170 --> 00:01:26,190
Now if we go one step further here and say Test Value and do the

27
00:01:26,190 --> 00:01:31,550
same thing, now you'll see it fails because it recognizes it as a

28
00:01:31,550 --> 00:01:37,030
text value or string value, and that's not allowed within a typed

29
00:01:37,030 --> 00:01:39,680
variable of type integer.

30
00:01:39,680 --> 00:01:40,110
Now,

31
00:01:40,110 --> 00:01:47,330
if we go a little bit further and say [string]$variable4 and

32
00:01:47,330 --> 00:01:52,340
then just say Test Value like so, then, of course,

33
00:01:52,340 --> 00:01:56,640
if I want to render that value, variable4, it works great.

34
00:01:56,640 --> 00:02:00,680
But if I change the value and get rid of the quotes and put a

35
00:02:00,680 --> 00:02:03,730
number 10, now notice what happens here.

36
00:02:03,730 --> 00:02:08,280
If I do variable4, it will render the number 10. So string

37
00:02:08,280 --> 00:02:11,060
will effectively take any kind of value.

38
00:02:11,060 --> 00:02:13,310
Now, are we 100% sure about that?

39
00:02:13,310 --> 00:02:14,380
Well, I'll tell you what we'll do.

40
00:02:14,380 --> 00:02:18,960
Let's do true as a value, because that's a separate value.

41
00:02:18,960 --> 00:02:20,580
That would be a Boolean value.

42
00:02:20,580 --> 00:02:25,040
Let's go back to variable4, and sure enough, it returns the Boolean value.

43
00:02:25,040 --> 00:02:28,110
So even though we strongly type variables,

44
00:02:28,110 --> 00:02:33,340
sometimes the string one will basically accept whatever has been typed in there.

45
00:02:33,340 --> 00:02:34,840
Now, if you remember,

46
00:02:34,840 --> 00:02:40,120
variable1 was a series of numbers, variable2 was the text

47
00:02:40,120 --> 00:02:43,190
value, and variable3 was an integer number.

48
00:02:43,190 --> 00:02:43,390
Now,

49
00:02:43,390 --> 00:02:48,090
if I didn't know that, what we have is the ability to do what's called

50
00:02:48,090 --> 00:02:52,790
GetType, and then I'm going to say Name and do Enter,

51
00:02:52,790 --> 00:02:57,940
and it will let me know what type of variable that is.

52
00:02:57,940 --> 00:03:00,190
So if I was unsure there's a variable coming in,

53
00:03:00,190 --> 00:03:03,660
I could say, well, GetType. If the type is the name of integer,

54
00:03:03,660 --> 00:03:05,340
then I know that that's a number.

55
00:03:05,340 --> 00:03:08,760
If I go and do the same thing for number 4,

56
00:03:08,760 --> 00:03:13,930
it will come back and say String, because that's the string one that we created.

57
00:03:13,930 --> 00:03:18,380
Now, of course, what happens when we want to kind of convert variables together?

58
00:03:18,380 --> 00:03:20,950
Then, of course, they need to be of the same type.

59
00:03:20,950 --> 00:03:25,610
So if I said variable, let's say 3,

60
00:03:25,610 --> 00:03:33,300
which was the number, + $variable4 and did that, it's

61
00:03:33,300 --> 00:03:36,230
going to come back with an InvalidArgument because one of

62
00:03:36,230 --> 00:03:39,050
them is a Boolean string value, which we set to True,

63
00:03:39,050 --> 00:03:40,490
and one is an integer,

64
00:03:40,490 --> 00:03:44,840
so you'll always get this error that's saying this is an incorrect format.

65
00:03:44,840 --> 00:03:48,440
Now, what we can do at this point is typely strong,

66
00:03:48,440 --> 00:03:51,850
like strongly type the variables even so that we can

67
00:03:51,850 --> 00:03:54,240
then adjust and add those together.

68
00:03:54,240 --> 00:03:59,340
So if I create some new variables, I'm going to say integer,

69
00:03:59,340 --> 00:04:03,840
and we'll call it num1, and I'll set that to 23.

70
00:04:03,840 --> 00:04:07,760
And then I forgot to put the dollar sign in there.

71
00:04:07,760 --> 00:04:09,330
It's always good that it lets me know.

72
00:04:09,330 --> 00:04:18,640
There we go. So integer and do $num2 = 45.

73
00:04:18,640 --> 00:04:28,680
Now what I can do is I can say $num1 + $num2, and I will get the answer of

74
00:04:28,680 --> 00:04:32,890
68, which is the sum from that. And I could do division, multiplication

75
00:04:32,890 --> 00:04:37,530
because the input values are specified as images.

76
00:04:37,530 --> 00:04:39,560
So when you're defining variables,

77
00:04:39,560 --> 00:04:42,490
you need to make sure that you define them as the right type so that

78
00:04:42,490 --> 00:04:45,740
then you can do conversion of that, if needed.

79
00:04:45,740 --> 00:04:48,520
Now, what we're going to do is go a little bit further, and

80
00:04:48,520 --> 00:04:51,120
we'll look at, well, how do we convert values?

81
00:04:51,120 --> 00:04:56,500
So let's say that we had var1 and var1 was set to

82
00:04:56,500 --> 00:05:02,250
01/01/2021, so just a text value.

83
00:05:02,250 --> 00:05:07,340
So if I say var, it will literally render just the text value.

84
00:05:07,340 --> 00:05:11,840
Now what about if I wanted to make that into an actual date?

85
00:05:11,840 --> 00:05:16,010
So what we can do here is we can prefix, so I'm going to use

86
00:05:16,010 --> 00:05:22,240
[DateTime]$var1 and do Enter. Now notice what happens.

87
00:05:22,240 --> 00:05:26,120
It converts it into the correct format for me, so

88
00:05:26,120 --> 00:05:29,130
January the 1st, 2021, which was the time.

89
00:05:29,130 --> 00:05:32,790
So I took a standard string value, and literally,

90
00:05:32,790 --> 00:05:37,280
by adding the type to the definition of the variable,

91
00:05:37,280 --> 00:05:41,140
it was able to then change to whatever that would be.

92
00:05:41,140 --> 00:05:47,210
Now if I go back and just say var1, notice it's still 01/01/2021 because I

93
00:05:47,210 --> 00:05:51,320
haven't changed that one. Now I could do it a different way.

94
00:05:51,320 --> 00:05:56,560
I could use that as, and I could say DateTime,

95
00:05:56,560 --> 00:05:58,410
and it will then return that value.

96
00:05:58,410 --> 00:06:04,040
But once again, if I go back to var1, it still retains the existing value.

97
00:06:04,040 --> 00:06:07,600
So when you're looking at converting values from one to another,

98
00:06:07,600 --> 00:06:10,510
whether it's a Boolean to an integer or a string to a

99
00:06:10,510 --> 00:06:13,590
number or a number or a string to a date,

100
00:06:13,590 --> 00:06:18,420
be careful how you do that because if you choose the wrong type,

101
00:06:18,420 --> 00:06:20,340
you can get the wrong value.

102
00:06:20,340 --> 00:06:23,150
And, of course, we've seen that in the past where, you know,

103
00:06:23,150 --> 00:06:29,940
I start with var2 and set that as a 1.2 as a value.

104
00:06:29,940 --> 00:06:33,330
So when I do var2, I get 1.2.

105
00:06:33,330 --> 00:06:38,130
But if I go and say integer for var2,

106
00:06:38,130 --> 00:06:41,620
I only get 1, and it loses everything else after it.

107
00:06:41,620 --> 00:06:43,960
I can then change that and say, well, actually,

108
00:06:43,960 --> 00:06:45,490
I want that to be a boolean,

109
00:06:45,490 --> 00:06:48,490
and it's going to return true because it has 1 at the beginning of it,

110
00:06:48,490 --> 00:06:51,540
even though it's an arbitrary value of 1.2.

111
00:06:51,540 --> 00:06:52,730
So just be aware,

112
00:06:52,730 --> 00:06:59,390
depending on the type of kind of value that you set for it, if I do string,

113
00:06:59,390 --> 00:07:00,710
it'll return the same value.

114
00:07:00,710 --> 00:07:04,700
If I go back and for some weird reason I choose datetime,

115
00:07:04,700 --> 00:07:07,340
it's going to give me a random datetime as well.

116
00:07:07,340 --> 00:07:12,390
So just be aware, getting the strongly typed values correct will

117
00:07:12,390 --> 00:07:19,000
dictate whether you have the right values returned to you for any other calculations that you want to use.

