I’ve been called many things throughout my life. My name is Todd. I also happen to be less than 7 feet tall so some might even call that short. I’ve been called names like Odd Todd, Tater Todd, Toddo Baggins, and some other unmentionables. What I haven’t been called though, is Todd Hawking. Although I have tons of theories about strings, they’re usually more to do with how they get into knots and cheese and stuff. Thankfully, to understand strings and string manipulation in PowerShell, you don’t have to be a theoretical physicist. In this post, we’ll go over some ways we can bend the strings to our will and pluck those bad boys like they were stretched out on a guitar.
\\\Theory of Everything
Actually…let’s start a little simpler than the Theory of Everything. How about the theory of something? In PowerShell, we can define a string by using the double quotation marks " "
, or the single quotation marks ' '
. Beyond just being useful in ASCII art, these 2 different characters have different implications when defining strings, especially when we get to more advanced strings. Let’s look at each one briefly.
–Single Quotation
This takes the literal characters in between the single quotes. This is when you don’t want any funny business to happen with your string. You don’t want to reference a variable or have any other surprises.
–Double Quotation
The double quotation mark takes characters but is also expandable and can pull variables and other expressions to be processed when defining the string.
–Triple Quotation…jk
This all makes more sense when you see it in action. Let’s define the variable $Word
with the value of fun and then use it in a string. One with single quotations and one with double quotations.

$Word
variable was substituted in the output. But if we do the same with single quotes, it’s literally the dollar sign and Word. Word, homie.
So now that we’ve laid the foundation for our theory, let’s get into some of the other dimensions and really assess the gravity of the situation. I know I can use a variable in my string with double quotations, but what if my variable isn’t a string and it’s a hashtable or PSCustomObject? How do I do that?? Does my theory hold water??
\\\$Dollar Hollar!
The Dollar $ign is good for more than just oversized bling around your neck. When working with strings, it can be used to give you the space you need to show your work. Use it with parentheses $()
to evaluate the expression inside when processing the string. Here’s how you would use it to pull the values from a hashtable and a PSCustomObject.
Hashtable:

PSCustomObject:

\\\Stranded
Okay, beyond basic strings, hashtables, and PSCustomObjects, there are some other black holes that you may encounter. These tripped me up when I first encountered them. They both are related to property names.
–Space Is A Problem
The first is that darn space that just messes everything up. How do we reference a property when it contains a space? Well, lets build on our previous string knowledge and make a ball of string so enticing cats would be envious. Here’s how you can reference a property that has a space in it when defining a string:

–A Period of Time
The strands are coming together! The second particle messing with us in when the property name contains a period. Since we’re referencing the value using dot notation, this can weigh on us like a graviton. For this example, I’m going to nest some properties so you can see what it does and where we get tripped up at.
First, let’s build our PSCustomObject with nested properties. Think of this as one of those 10k page bills Congress is trying to pass. We have no idea what’s in there, but just press enter anyways. We can do this in PowerShell by combining a hashable and a PSCustomObject:
[PSCustomObject]$Table = @{Theory = [PSCustomObject]@{'Name.Of.Theory' = 'String'}}
Name.Of.Theory
by name, nothing is returned. But if we wrap it in single quotes 'Name.Of.Theory'
, PS can do the rest:
Man, we sure are straightening this string stuff out, aren’t we? Check out Part II where we go over more advanced use cases with strings: expressions, adding strings together, the escape character, and combining it all together to build an expression to use with the Invoke-Expression
cmdlet. Check it out you mad scientist you!