Powershell

Understanding How PowerShell Variables in Strings Work

A variable in PowerShell is a unit of memory that stores different values such as strings, integers, or booleans. It begins with a dollar sign ($) in PowerShell. In contrast, the string is typically a sequential combination of characters that makes a meaningful text. Variables can store strings and also can be substituted inside a string. Moreover, string-assigned variables can be utilized to concatenate strings.

The following article will discuss techniques related to working a variable in strings.

How PowerShell Variables Work in Strings?

There are many ways to use the variables inside a string. These include the concatenation of strings, the substitution of variables inside strings, or storing system cmdlets inside strings with the help of variables.

Example 1: Concatenate a String With a Single Variable

This following example will help concatenate the string with a variable:

$name = "John"
$msg = "Hello, Mr. " + $name
$msg

According to the above code:

  • First, initialize the “$name” variable and assign the string value “John” to it.
  • Initiate another variable, “$msg”, then assign the variable “$name” to it and concatenate with the “Hello, Mr. ” string:

Example 2: Concatenate a String With Multiple Variables

Check out concatenation of the two string-assigned variables:

$f_name = "John"
$l_name = "Doe"
$msg = "Hello, Mr. " + $f_name + " " + $l_name
$msg

According to the above code:

  • First, initialize the two variables “$f_name” and “$l_name”.
  • Then, assign them values with first name and last name.
  • After that, initiate another variable, “$msg”, assign it a string, and two variables are defined above.
  • Lastly, concatenate them by adding a “+” operator between them and call the “$msg” variable to show the output:

Example 3: Substitute a Variable Inside a String

The following example will substitute string-assigned variables within a string:

$f_name = "John"
$l_name = "Doe"
$msg = "Hello, $f_name $l_name"
$msg

In the stated code above:

  • After creating string assigned variables “$f_name” and “$l_name”, create another string.
  • First, write a word, substitute the two string-assigned variables, and assign it to the “$msg”.
  • Finally, call the variable “$msg” to show the output:

Example 4: Substitute a Command in String With a Variable

This example will teach you how to substitute a command inside a string by using a variable:

$dir = Get-Item 'C:\Doc'

$msg = "Time: $($dir.CreationTime)"
$msg

According to the code above:

  • First, initiate a variable “$dir” and assign “Get-Item” cmdlet followed by the directory address.
  • Then, initiate another variable, “$msg” and assign a string.
  • Inside the string, the “Time:” word is first added, then a dollar sign is specified outside the curly braces, making it a variable.
  • Inside the curly braces, the “$dir” variable is concatenated with the “CreationTime” cmdlet.
  • Lastly, invoke the “$msg” variable to get the creation time of the directory:

Example 5: Execute a System’s Command by Storing it in the Variable

This example will run a system’s command inside a string that is stored in a variable:

$msg = "Current date and time is: $(Get-Date)"
$msg

According to the above-stated code:

  • First, initiate a variable “$msg” and then assign a string to it.
  • Inside a string, add some text first, and then invoke a command “Get-Command” within curly braces.
  • Lastly, invoke the “$msg” variable to show the output in the console:

That was all about using variables in strings.

Conclusion

The variables in PowerShell strings work in various ways. The strings are first stored in a variable then those string-assigned variables help concatenate them with the aid of the “+” operator. Moreover, the string-assigned variables can be substituted inside another string. This blog has covered details about the working of the variables in strings in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.