So to get started, let's talk about converting and formatting data values.
This is a common task that when building PowerShell
scripts that you'll have to do,
which will be converting a number to something else or a text value to
something else or maybe going a little bit deeper into,
for example, .NET typed objects.
So first off, what type of variables are there?
Well, there's what's referred to as loosely typed.
These are when a value is assigned to effectively an undefined type of variable.
Now as you've seen so far in the previous modules,
we just create a variable called $variable and assign a value.
That would be a loosely typed variable.
The second option is a strongly typed variable.
This is where you are assigning the type to that variable.
So, for example, this variable will be a integer,
or this will be a string, and the only type of data that it can contain is that.
We can use variables to store data.
We can store all types of values within PowerShell variables.
A variable is just a unit of memory in which those values actually get stored.
We then declare those variables by just using the dollar sign before the name.
Variable names aren't necessarily case‑sensitive,
and they can include spaces and special characters.
But I will advise you to try and keep them without spaces
and special characters and just create them with dollar and
a name that makes sense to you.
Now there are different variable types available to us.
The first is user‑created variables.
So we just go in and create them,
and they're available to us in the context of the
PowerShell script that we're executing.
We then have what's referred to as automatic variables,
and we'll talk a little bit more about this.
And then, of course, we have preference variables.
Preference variables are really around some of the configuration around the
PowerShell script itself or the execution and things like that.
So first off, we have user‑created variables.
These are created and maintained by you as the user.
The variables created within the command line exist
only in the context of that window.
So if I open up a PowerShell console and create $variable equals 10,
then I can't close that window and reopen it and
expect to find that same value there.
The automatic variables store the state of PowerShell.
Now PowerShell create these variables and changes their values as required,
and you can execute this.
So, for example, we can use a $ps, for example,
and let's say environment or env, so psenvironment.
It would be an automatic variable that we can access.
Then, of course, we have preference variables.
So they are variables that store user preferences for PowerShell.
These variables, once again,
get created by PowerShell and get populated with default values.
However, they could be changed.
So how do we create user variables?
Well, it's as simple as doing this, dollar,
the name, equals, and set it to a value.
So you can see I have three variables here.
One is set to a comma‑separated set of numbers.
One is a string value.
One is a string data value.
Now don't get confused here.
Just because it's in a date format does not make that variable date type.
It's just a string.
Now if I wanted to create a typed variable,
I can do this a little bit differently.
This is where we define what that variable can contain.
So you'll see my variable 1 is now going to contain an integer.
So in square brackets, I type Int,
which is the strong type of what I wish to add in there.
The same would go for the DateTime.
Now there are common variable data types that we can
utilize outside of int and datetime.
We have boolean, so the yes/no value, a date time.
We also have a PowerShell object, and we'll talk about those a little bit later.
We then have a script block, which is obviously other executed PowerShell,
a string value, which is obviously the most common,
and then, of course, an array of objects.
Now once we start grabbing data and populating it into variables,
one of the most common things we have to do is to convert or
what's referred to as cast those data values.
Casting is about converting one object type to another so
that we can utilize it into something else.
Not all objects can be cast,
so you can't just randomly say I want this type of
value to be cast to something else.
You have to be careful.
So converting objects from one to another,
often you would mitigate some of these issues by just defining the
variables with the right types to begin with.
So let's look at how we would cast variables and values.
So I have two variables here.
Variable 1 is the number 1.
Variable 2 is clearly a date, 01/01/2021.
If I want to convert a string variable to an integer,
I'm going to prefix the variable with Int as the strong type.
If I want to convert the string variable to a date,
I need to put DateTime in front of that specific variable.
If I want to convert a false value to an integer,
so a false or a true, I can simply put int before it,
and it will then give it its either 0 or 1 value.
When a value is cast to a particular datatype, it's a one‑time change.
So it doesn't have to happen all the time.
It's just I converted it and changed it at that point.
When a variable is cast to a particular datatype,
it will stay that way unless it's updated again.
So just think that through.
If we go back one slide, when a value is cast to that
datatype, it's a single transaction.
When a variable is cast, it will stay that way.
So if you define a variable as an integer,
it will always stay an integer unless you update.
Now what we do have is we have an operator called the ‑as operator.
This will allow you to cast things to a specific thing. Now it also
serves the purpose for being able to test a conversion. So you can use
that as operator to say, well, if I convert this to that,
will it work?
You can also define the type after the variable by using the as command.
And then, of course,
the big thing here to worry about is that it can have unexpected results.
It may not work the way that you're expecting because,
as I mentioned before,
you cannot cast everything into anything, and the as is such
an easy way of doing it that it doesn't necessarily work the
way that you're expecting.
So what's the syntax look like?
Well,
let's go back and have the same two variables, variable1, string
value number, variable2, date string value.
If I want to cast it as an integer, I can simply say
variable1 ‑as using the operator Int.
If I want to do the date, I can do the same process and say as date.
And if I wanted to do the same thing as I did before,
so casting the false value to an integer, I can do false as an
integer. So a couple of different ways of doing it.
It really depends how you want to write the PowerShell script. I like to use
the defined variables just because it kind of makes more sense that way that
you control the input to get what you want.
But if you're kind of ad hoc, on the fly,
you may not want to have input or there's no input that
comes in, this may be the output of something else, then
you can use that as operator.
Now we also have what's called the f operator.
This is used to format a string expression.
So instead of us casting objects, we can format the values into something else.
This supports complex formatting. And you can begin the statement
with the selected format, so what you would like that to be in. So
let's look at how we format the data values.
So what does this look like in PowerShell?
So I have two variables here.
You can see that one is a 123 point something,
and then the other variable is just a standard number.
It actually looks like a phone number.
If I want to display only the three decimal places,
you can see that we can use this kind of syntax.
Now remember,
we said we put this at the beginning of the variable. So you'll
see that in quotes, in brackets, it will be 0: and then 0 being
the actual value that's coming in.
And then you've got n3, which will tell it that I only
want to show the three decimal places, and then the ‑f is telling it to format.
If I want to convert variable2 into a phone number with spaces,
same principle again.
You do this before the variable,
and it will be squirrely brackets, 0 is the value coming in,
and then the colon is the format on the right. And you can see, I'm
doing 3‑3‑4. If I want to display the year only from the current
date, I can do this and say 0: year, year,
year year, which will then, I'm using not a variable.
I'm using a command called Get‑Date,
which will get me the current date of today, but only
return back the year. Now of course,
dates are the most common one that you would see where
you would be formatting as in month, month ‑d, d for day, ‑year,
year, year.
So that's a common use case, but it works for numbers, as well as regular text values.