EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Variable in String
 

PowerShell Variable in String

Updated March 13, 2023

PowerShell Variable in String

 

 

 

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Powershell Variable in String 1

When we use this variable inside the string, let see what output it produces.

Code:

Write-Output "Representing $num to string"

Output:

Powershell Variable in String 2

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:

Powershell Variable in String 3

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:

double-quote

Code:

'Expanding Number: $num variable with the single-quote'

Output:

single-quote

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:

Powershell Variable in String 6

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:

Powershell Variable in String 7

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:

Using the variable inside the Here-String

With single-quote.

Code:

@'
This is Here-String
First Variable: $val1
Second Variable: $val2
'@

Output:

With single-quote

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:

variable inside the string

When we expand the service name.

Code:

Write-Output "Service Name is $ser.Name"

Output:

Powershell Variable in String 12

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:

Powershell Variable in String 13

Or you can use the string formatting methods as shown below.

Code:

"Service name is : {0}" -f $ser.name

Output:

Powershell Variable in String 14

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:

Powershell Variable in String 15

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:

Invoke-Command

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:

Powershell Variable in String 17

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:

inside the hashtable

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:

Representing the array

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 –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW