PowerShell String Manipulation for Dynamic Code Generation

PowerShell enables dynamic code generation by constructing and executing commands on-the-fly. Achieve this using string manipulation and the Invoke-Expression cmdlet.

Brien Posey

June 24, 2024

4 Min Read
conceptual art showing a dynamic vortex shape and binary code
Alamy

Some of the more complex scripts I have written for ITPro Today required me to explore dynamic code generation, which involves having PowerShell generate and execute a command on-the-fly, rather than hardcoding the command within the script.

There are many potential use cases for dynamically generated code. I often use it as a workaround when two or more data sources (or variables) do not work together as needed. In such situations, I might capture the contents of the variables I am working with, assemble a string containing the command I want to use, and include the various values I want to use with that command. Admittedly, this sounds like an odd technique, but it has saved me in quite a few situations where conventional methods fail.

The basic idea behind dynamic code generation is to create a variable containing the desired command. If you display the variable, PowerShell will show you the command. However, if you enter the Invoke-Expression cmdlet, followed by the variable name, PowerShell will execute the command stored within the variable. Figure 1 shows a simple example of this.

String_Manipulation_1-1.jpg

Figure 1. I used the Invoke-Expression cmdlet to execute the command stored within the $Example variable.

PowerShell String Manipulation Techniques

Since strings can help troubleshoot stubborn code blocks, I wanted to demonstrate some PowerShell string manipulation techniques.

Related:How To Use Dynamic Parameters in PowerShell Functions

Single vs. double quotes

When working with strings in PowerShell, your choice of single or double quotes matters – or at least it does if you plan to do any string manipulations. Here are two ways to declare a string variable:

$String1 = “This is an example of using double quotes”
$String2 = ‘This is an example of using single quotes’

Both commands are perfectly valid. If you output the contents of either variable, you will see the string displayed. You can see this in the upper portion of Figure 2.

However, things get interesting when incorporating a variable within a string. If you use single quotes, PowerShell displays the variable name, not its contents. To see the variable contents, you must use double quotes.

For the sake of demonstration, return to the pair of strings above. Both contain the phrase: This is an example of using. Let’s incorporate that phrase into a variable and then reference it from an abbreviated version of our original strings. Here are the commands:

$String3 = “This is an example of using“
$String1 = “$String3 double quotes”
$String2 = ‘$String3 single quotes’

Here, $String3 equals the phrase: “This is an example of using”. I then referenced that variable in $String1, which illustrates the use of double quotes, and $String2, which demonstrates the use of single quotes. In Figure 2, outputting $String1 causes the full text to display correctly because I used double quotes. When I display the output of $String2, which uses single quotes, I see the name of the $String3 variable rather than the text contained within that variable.

String_Manipulation_1-2.jpg

Figure 2. If you plan on manipulating strings, it’s best to use double quotes when defining a string variable.

String concatenation

String concatenation is a simple type of string manipulation that joins two strings together. I use this technique often when dynamically constructing and executing commands.

In the previous example, $String1 references $String3 during its creation. In doing so, $String1 contains the contents of $String3. However, we can also create two different strings and concatenate them.

There are several ways to concatenate strings, but I often use a plus sign to combine variables or text blocks. Here is an example:

$String1 = “This is an example of”
$String2 = “joining two strings together”
$String3 = $String1 + $String2

Running these commands and displaying $String3 reveals that the strings join without a space between “of” or “joining.” To fix this, add a space to the end of $String1 or use this command:

$String3 = $String1 + “ “ + $String2

String_Manipulation_1-3.jpg

Figure 3. I have combined two strings.

Having shown you how to declare and concatenate strings, I will demonstrate additional string manipulation techniques in a follow-up article. In that article, we will cover converting a string into an array, inserting/removing portions of a string, and using single vs. double quotes in further detail.

Learn more about PowerShell strings:

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like