Updated March 13, 2023
Introduction to PowerShell Variable in String
Variables in a PowerShell is a way to store the values inside them, and using these variables in a string is a method to represent those variables by expanding it in a double quote and also representing variables in different ways like string formatting or direct PowerShell variable and performing operations for the variable inside the string when they store the cmdlet values and normal operation like the string concatenation.
Syntax of PowerShell Variable in String
There are no specific methods for the variable in a string, but there are ways when the string is declared we can expand the variables inside.
For example, declaring string variables outside and using them inside the double quote, single quote, here-string, PowerShell string array, or representing them in the Invoke-Command.
Description:
PowerShell declares a variable by using the $ sign and the variable name.
For example, $Services. When we declare any type (Integer, double, string, DateTime) of the variable and when we use them inside the string variable, it automatically converts that variable to the string.
Code:
$num = 20
$num.GetType()
Here, we have declared the $num variable, and its value is 20, and its datatype is an integer, as shown in the below output.
Output:
When we use this variable inside the string, let see what output it produces.
Code:
Write-Output "Representing $num to string"
Output:
Although the number is 20, it was represented inside a string, but its datatype is preserved unless it is explicitly converted to the string. Similarly, for the other data types, the string can convert any variable to the string.
Code:
$a = 10.2
Write-Output "Representing float $a to string"
$b = "Hello World"
Write-Output "Representing String $b to string"
Output:
Single-quote and Double-Quote in the PowerShell are the same, but expanding the variable makes a huge difference because single-quote string can’t expand the variable.
Code:
"Expanding Number: $num variable with the double-quote"
Output:
Code:
'Expanding Number: $num variable with the single-quote'
Output:
There are many other ways we can use the variable in a string.
Examples of PowerShell Variable in String
Given below are the examples of PowerShell Variable in String:
Example #1 – Displaying variable with the string formatting.
When there are variables to display in the string, we can display them using the string formatting method -f with ‘{}’.
We have the two variables in this example and how generally we display them.
Code:
$a = "Hello world"
$b = "Microsoft Azure"
Write-Output "First Variable: $a, Second Variable: $b"
"Without Write-Output, First Variable: $a, Second Variable: $b"
Output:
With the formatting method, we can display the variables as shown below.
Code:
"Formatting method: First Variable: {0}, Second Variable: {1}" -f $a, $b
Output:
Example #2 – Using the variable inside the Here-String.
When we expand the variables inside the Here-String @” ”@, the use of Single or Double quotes matters. When we use the single quote, it can’t get the value of the variable, but with the double-quote, it is possible.
Code:
$val1 = "Hello world"
$val2 = "Microsoft PowerShell"
@"
This is Here-String
First Variable: $val1
Second Variable: $val2
"@
Output:
With single-quote.
Code:
@'
This is Here-String
First Variable: $val1
Second Variable: $val2
'@
Output:
Example #3 – Using the cmdlet output with a variable inside the string.
We can use the output of the variable inside the PowerShell string, and we can also expand their properties.
Code:
$ser = Get-Service -Name WinRm
$ser
Output:
When we expand the service name.
Code:
Write-Output "Service Name is $ser.Name"
Output:
We could get the output of the service, but if you notice, it is the type of the service name, not the actual service name, so if you want the proper output, we need to use the $ variable before using the service variable.
Code:
Write-Output "Service Name is $($ser.Name)"
Output:
Or you can use the string formatting methods as shown below.
Code:
"Service name is : {0}" -f $ser.name
Output:
Let’s take another example of expanding the cmdlet output variable inside the here-string.
Code:
@"
Today's date is $(Get-Date)
Today's Day is $((Get-Date).DayOfWeek)
Today's Day of the year is $((Get-Date).DayOfYear)
"@
Output:
Example #4 – Using the variable inside the Invoke-Command string.
Using the Invoke-Command, when we use the variable inside the string and get the value that is out of the Invoke-Command scope, we can’t get its value.
Code:
$value = "2021"
Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock {
Write-Output "The outside variable is: $value"
}
Output:
This is not the problem with the string or the variable, but the variable is outside of the scope. So for this method, we need to pass it using the -ArgumentList parameter.
Code:
$value = "2021"
Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock {
param(
$value
)
Write-Output "The outside variable is : $value"
} -ArgumentList $value
Output:
Example #5 – Using the Variables in a string inside the hashtable.
We can also use the variables in a string inside the hashtable. In the below example, we can directly use the variable in the value field but just to show that the expansion of the variable supports in the string supported in the hashtable.
Code:
$Ser = Get-Service -Name WinRm
@{
ServiceName = "$($Ser.Name)"
Starttype = "$($ser.StartType)"
Status = "$($ser.Status)"
}
Output:
Example #6 – Representing the array in the PowerShell string.
We can use indexing to get the output of the array using the string.
Code:
$services = @("Winrm", "Spooler", "w32Time")
for($i=0; $i -lt $services.Count; $i++){
"Service Name : $($services[$i])"
}
Output:
We can also use the foreach method.
Conclusion
Variables in the PowerShell string are very useful when we store the output to the file when we want to minimize the efforts by expanding the variable of the cmdlet inside the string instead of using the additional variables for cmdlet expansion which makes them easy to display and format the values inside the string.
Recommended Articles
This is a guide to PowerShell Variable in String. Here we discuss the introduction to PowerShell Variable in String along with examples. You may also have a look at the following articles to learn more –