PowerShell For Programmers: Strings, Quotes and Quirks

Kory Thacher

Welcome back everyone! This will be a short, but important entry for the guide.

The difference between quote characters is something I’m asked about all the time. It is important to understand in PowerShell, but most of the time it probably won’t make a difference.

TLDR: If you’re just typing a value like “alg” then it doesn’t matter, but if you have any special symbols you can see the difference.

Single vs. Double Quotes

We refer to single quotes as Literal Strings and double quotes as Expandable Strings. Both create the same datatype (system.string), but a double quote will expand any special character and a single quote will use the literal characters you typed.

$a = "test"

"A is $a"

'A is $a'

 

A is test
A is $a

 

Variable Sub-Expressions

With an expandable string, you might run into some unintended behavior when you try to access a property of a variable (or do some other kinds of special actions). In these scenarios we need to use a variable sub expression or string concatenation (+ sign concatenates and so does -join). A sub expression uses $() to surround the special code we want to run inside our expandable string:

$service = get-service alg
"Service: $service"
Service: System.ServiceProcess.ServiceController

That doesn’t look right!

When PowerShell isn’t sure how to convert something to a string, it just shows you the data type you gave it.

“No problem, I’ll just access the property I want!” ~Every programmer ever

 

"Service: $service.Name"

 

Service: System.ServiceProcess.ServiceController.Name

Not quite what we were expecting Sad smile

"Service: $($Service.Name)"
Service: alg

 

Eureka! Open-mouthed smile

This allows us to run code as well:

"Date: $(get-date)"

 

Date: 01/24/2018 11:01:15

 

Now this solves the problem and allows us to write properties of our objects into strings.

Simple String Manipulation

As I mentioned, concatenation also works. We can concatenate strings via the + sign, or –join operator.

"Service: " + $Service.Name
"Service",$Service.Name -join ": " #you can join with any delimeter

We also have operators and methods for split and replace. The operators use Regex and the methods do not. (I can do a post on regex in the future)

"a,b,c,d,e" -split ","
"a,b,c,d,e".Split(",")

"a,b,c,d,e" -replace ",",";"
"a,b,c,d,e".Replace(",",";")

Feel free to pipe any string into Get-Member to see some of the other methods you might be familiar with and looking for.

Format Operator

The format operator gets some pretty heavy use in C# and it lives here in PowerShell as well. If you’re unfamiliar with it, the idea is that we want to put placeholders in our string and populate them with values from an array later.

So, if we wanted to write out a bunch of comma-separated properties of an object we could do something like this:

# -f
"{0},{1},{2}" -f $service.DisplayName,$service.StartType,$service.Status

 

Notice the place holders are a set of curly braces {} with an index number inside. They don’t have to be in order, and they can be reused.

We can also force specific format codes onto the data:

#c is the code for currency
"Cost: {0:c}" -f 502342420.12

you can find the other codes here, but its a bit annoying to get through.

I personally don’t like to use the format operator very often, but it can do some pretty unique and impressive things when you need it. I found this blog post to have a great reference about extra format operator information so I figured linking it would be better than reinventing the wheel.

 

Well that’s all for now, hopefully this helps you on your PowerShell journey and helps you determine which quotes you need to use as well as get some useful manipulation done. I’ll be addressing another kind of string, called a “Here” string in a short section later.

I’ll be adding to the series to cover other quirks as well so let me know in the comments if there is any particular topics confusing you!

If you find this helpful don’t forget to rate, comment and share 🙂

For the main series post, check back here.

0 comments

Discussion is closed.

Feedback usabilla icon