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.

You can see the value of the $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.




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'}}
The issue we run into is the period when referenced using dot notation will try to get a sub property. In our case, the name or key contains periods. If we just try to reference Name.Of.Theory by name, nothing is returned. But if we wrap it in single quotes 'Name.Of.Theory', PS can do the rest:
Toast Notification 3

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!


Nested AD Managers (PowerShell)

So I had a somewhat odd request (isn't that how all truly great stories start) a long time ago in an IT galaxy far, far away. And it went something like this: we want to email an employee and CC their manager, but if their manager is inactive (took a one-way trip to...

6 Things I Wish I Had Known About PowerShell

About a hundred years ago when I stopped riding dinosaurs to work and started learning PowerShell, I struggled to know what to learn first and where to even begin. That blue box with white writing was intimidating to say the least. I finally worked up the courage to...

Index Scripts for Windows Search

So you just finished writing some code and you go to save your file. You summarize all of the important aspects this section of code contains into a nice, easy-to-read file name that your future self will immediately recognize. Fast forward to the future where you...

Array vs ArrayList (PowerShell)

For some tasks in life, being precise is a necessity. But most of us get away with rounding, paraphrasing, and hitting in the general vicinity most of the time. Depending on your personality, you may be one who strives for perfection and strains on every miniscule...

Spice Up HTML Emails with PowerShell – Part III

So far in this series we've fumbled our way around the kitchen and tried to wing it when sending HTML emails with PowerShell. It was clunky to say the least. We then went through our spice rack and built an HTML template, highlighting the nuances of each spicy element...