So let's go into the PowerShell console,
and we'll look at how we can convert and format values.
We'll look at base formatting of values first, then we'll
cast values to a different type, and then we'll look at how
we can cast using the AS operator.
So we just talked about variables, so let's have a look
at the practicalities of this. Well, first off,
let's create a variable called variable1,
and what we'll do is just add some specific numbers like so.
If I want to view what's in the variable,
then I can just obviously type variable.
Now if I also declared a variable2, and maybe I just did a string
value, so let's imagine it's a path to C:\Documents like so, I can
then do variable2 and render that value.
So nothing spectacular there.
Those are just called regular basic variables that don't have any types.
Now,
if we were to change this and I wanted to make sure
that the variable had a specific type,
so if I say variable3 and I set that as an integer with a 0,
then, of course, when I do variable3, it'll just return 10.
Now what about if I change that and put the 10 in quotes like so?
Is it going to return the same value?
Yes, it does.
It returns a 10.
Now if we go one step further here and say Test Value and do the
same thing, now you'll see it fails because it recognizes it as a
text value or string value, and that's not allowed within a typed
variable of type integer.
Now,
if we go a little bit further and say [string]$variable4 and
then just say Test Value like so, then, of course,
if I want to render that value, variable4, it works great.
But if I change the value and get rid of the quotes and put a
number 10, now notice what happens here.
If I do variable4, it will render the number 10. So string
will effectively take any kind of value.
Now, are we 100% sure about that?
Well, I'll tell you what we'll do.
Let's do true as a value, because that's a separate value.
That would be a Boolean value.
Let's go back to variable4, and sure enough, it returns the Boolean value.
So even though we strongly type variables,
sometimes the string one will basically accept whatever has been typed in there.
Now, if you remember,
variable1 was a series of numbers, variable2 was the text
value, and variable3 was an integer number.
Now,
if I didn't know that, what we have is the ability to do what's called
GetType, and then I'm going to say Name and do Enter,
and it will let me know what type of variable that is.
So if I was unsure there's a variable coming in,
I could say, well, GetType. If the type is the name of integer,
then I know that that's a number.
If I go and do the same thing for number 4,
it will come back and say String, because that's the string one that we created.
Now, of course, what happens when we want to kind of convert variables together?
Then, of course, they need to be of the same type.
So if I said variable, let's say 3,
which was the number, + $variable4 and did that, it's
going to come back with an InvalidArgument because one of
them is a Boolean string value, which we set to True,
and one is an integer,
so you'll always get this error that's saying this is an incorrect format.
Now, what we can do at this point is typely strong,
like strongly type the variables even so that we can
then adjust and add those together.
So if I create some new variables, I'm going to say integer,
and we'll call it num1, and I'll set that to 23.
And then I forgot to put the dollar sign in there.
It's always good that it lets me know.
There we go. So integer and do $num2 = 45.
Now what I can do is I can say $num1 + $num2, and I will get the answer of
68, which is the sum from that. And I could do division, multiplication
because the input values are specified as images.
So when you're defining variables,
you need to make sure that you define them as the right type so that
then you can do conversion of that, if needed.
Now, what we're going to do is go a little bit further, and
we'll look at, well, how do we convert values?
So let's say that we had var1 and var1 was set to
01/01/2021, so just a text value.
So if I say var, it will literally render just the text value.
Now what about if I wanted to make that into an actual date?
So what we can do here is we can prefix, so I'm going to use
[DateTime]$var1 and do Enter. Now notice what happens.
It converts it into the correct format for me, so
January the 1st, 2021, which was the time.
So I took a standard string value, and literally,
by adding the type to the definition of the variable,
it was able to then change to whatever that would be.
Now if I go back and just say var1, notice it's still 01/01/2021 because I
haven't changed that one. Now I could do it a different way.
I could use that as, and I could say DateTime,
and it will then return that value.
But once again, if I go back to var1, it still retains the existing value.
So when you're looking at converting values from one to another,
whether it's a Boolean to an integer or a string to a
number or a number or a string to a date,
be careful how you do that because if you choose the wrong type,
you can get the wrong value.
And, of course, we've seen that in the past where, you know,
I start with var2 and set that as a 1.2 as a value.
So when I do var2, I get 1.2.
But if I go and say integer for var2,
I only get 1, and it loses everything else after it.
I can then change that and say, well, actually,
I want that to be a boolean,
and it's going to return true because it has 1 at the beginning of it,
even though it's an arbitrary value of 1.2.
So just be aware,
depending on the type of kind of value that you set for it, if I do string,
it'll return the same value.
If I go back and for some weird reason I choose datetime,
it's going to give me a random datetime as well.
So just be aware, getting the strongly typed values correct will
dictate whether you have the right values returned to you for any other calculations that you want to use.