Finally,
let me show you some other ways that you can format
strings and information in PowerShell,
as well as creating custom output that does exactly what you need.
Let's get the bits service, my favorite service to work with.
Now we looked at this earlier in the course where if I want to build a
string that says the bits service is running or is stopped,
we looked at ways of using sub expressions instead of using concatenation.
I'm going to show you another way, and that is using the ‑f operator,
or the format operator.
In the course downloads,
I have some links to some Microsoft documentation that will
explain in a bit more detail what I am doing.
So I'm going to create a string, and the string has placeholders,
0 and 1 in curly braces.
So the string is on the left side of the ‑f operator.
On the right side of the operator, I have a comma‑separated list of values.
So I'm getting the name property of the service
object and then the status property.
Those go into the corresponding placeholders 0 and 1.
They don't have to be, by the way, in order.
If I added something later on, and it was 2,
like that could go in front of the 0.
Typically though,
try to keep them in order as you're getting used to this
because it'll be easier to keep things straight.
Now I get the string that says bits is stopped.
The name and status plugged into the placeholders 0 and 1,
and I get the string, bits is stopped.
This may be an easier way to build strings for messages
that you want to display to a user.
This may be an easier way to build strings that you might
want to use in your own PowerShell work.
Now we can also use this ‑ f operator and format things like dates and numbers.
So let's format a number.
There is an f qualifier that we can use.
So I'm going to take the number that's on the right side,
1234.56 and so on, that is going to be plugged into the 0 placeholder,
and then the f qualifier says treat this as a fixed number.
By default, it truncates it to two decimal points.
Now it's very important that you recognize here
that when you use the ‑f operator,
PowerShell is writing a string to the pipeline.
So even though this looks like a number, this is actually a string.
So if you were to use this code in a, say,
select object expression, and try to sort on the value,
it's going to sort it as a string, not as a number.
Here's another example,
I'm going to format the value as a fixed number with no decimal points,
again, this is going to be a string, or I can say,
you know what, I need three decimal points,
whatever you need.
Use the n operator, and these qualifiers, by the way, are case sensitive.
Using the n qualifier has a nice effect of giving you comma output.
A p will be a percentage, and this includes the percent symbol.
Again, this is kind of a nice thing to have,
makes your output maybe a little bit easier to read.
If you need to specify the number of decimal points,
you can do p and then the number of decimal points.
So I'm going to do this to 4, and there is the output.
There is also the ability to display or format a value in a currency,
and the currency will default to whatever your
regional or international settings are.
So in my case, it's the dollar sign, and I get $23.56.
These are kind of fun things to play with.
We also have similar things that we can do with datetime objects.
So let's create a datetime object $d,
and I'm going to format this using the d qualifier.
Now these qualifiers that you're seeing here,
the lowercase,
these are the same values that you can use in the
dash format parameter for Get‑Date.
So you can see this gives me just a date string.
Capital D, remember I said they're case sensitive, gives me a long date string.
There are methods on the datetime object we've looked at
that will also give you the same result.
Whether you use the method or the ‑f operator really depends
upon the context of what you are trying to do.
A lowercase g gives me a datetime string.
Again, these are all strings, and f gives it to me with a little more detail.
Lowercase u gives me that UTC string,
and I can also format with a case‑sensitive string like this,
year, day, month, the month is capital M,
minutes would be lowercase m.
So I'm trying to build a string, say, for a file name or a directory name,
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.