Select Page

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.

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'}}
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!

OTHER POSTS YOU WANT TO READ

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...

Spice up HTML Emails with PowerShell – Part II

In Part I of our scrumptious concoction we put our script into the oven to let it bake. But we forgot to add our secret sauce that's sure to leave our recipients drooling, which is clearly our goal. In this post we'll continue to spice up HTML emails with PowerShell...

Spice Up HTML Emails with PowerShell – Part I

I live in the South, specifically in God's country AKA TEXAS BABAY! There's plenty of amazing things about Texas and definitely some reasons to loathe being from Houston, but it's hard to knock our food. I mean, is there really even a debate between Whataburger vs...

Disable O365 Apps in License (PowerShell)

If it's free, it's better. This is basically my mantra for life and a really easy one to follow at that. You can apply it to all sorts of things: ice cream, candy, food. Okay, maybe just free food tastes better when you know you didn't have to pay for it. Especially...

Font Installer Script (PowerShell)

Things have smells. I know, this is a revolutionary idea and you may need to stop right here and take a breather while you process this massive revelation. Things that are brand new have a particular smell that vanishes shortly after hitting your nostrils. It's...
%d bloggers like this: