While working on an automation script, I needed to convert a variable to a string in PowerShell. I tried different methods and thought I’d share. In this PowerShell tutorial, I will show you how to convert variables to strings in PowerShell.
To convert variables to strings in PowerShell, you can use the ToString() method. This method can be called on any variable to convert its value to a string. For example, if you have an integer variable $number = 123, you can convert it to a string by using $stringNumber = $number.ToString(). This will store the string “123” in the $stringNumber variable. The ToString() method ensures that the variable is explicitly converted to a string format.
Why Convert to String in PowerShell?
Strings are data types used for text manipulation, logging, and message display. Converting variables to strings is often necessary when concatenating text, formatting output, or passing data to functions that expect string input. Else you might get runtime errors while running the script.
Convert Variables to Strings in PowerShell
Let’s check various methods and cmdlets for converting a string to a variable in PowerShell. I will show the complete script with examples.
1. Using the ToString() Method
The ToString() method is used to convert a variable to a string in PowerShell. This method is available on most .NET objects and can be called directly.
Here is a complete example. Have a look below:
# Convert an integer to a string
$integerVariable = 123
$stringVariable = $integerVariable.ToString()
Write-Output $stringVariable
# Convert a date to a string
$dateVariable = Get-Date
$stringDate = $dateVariable.ToString()
Write-Output $stringDateI executed the PowerShell script, and you can see the output in the screenshot below.

Read Split Strings by Newlines in PowerShell
2. Using String Interpolation
Here is another method for converting a variable to a string in PowerShell: string interpolation.
String interpolation allows you to embed variables directly within a string.
Here is a complete example:
# Convert multiple variables to a string
$firstName = "John"
$lastName = "Doe"
$age = 30
$stringVariable = "$firstName $lastName is $age years old."
Write-Output $stringVariableThe screenshot below shows the output; you will also get the same output when you run the above script.

3. Using the -f Format Operator
In PowerShell, you can also use the -f Format operator to convert a variable to a string.
The format operator -f provides more control over string formatting, allowing you to specify the exact format for each variable.
Here is a complete PowerShell script:
# Format a string with variables
$firstName = "Jane"
$lastName = "Smith"
$age = 25
$stringVariable = "{0} {1} is {2} years old." -f $firstName, $lastName, $age
Write-Output $stringVariable Read How to Filter Strings in PowerShell?
4. Using Out-String Cmdlet
The Out-String cmdlet converts objects to their string representation. This method is useful when dealing with arrays or complex objects.
Check out the below example.
# Convert an array to a single string
$arrayVariable = @("New York", "Los Angeles", "Chicago")
$stringVariable = $arrayVariable | Out-String
Write-Output $stringVariable
# Output:
# New York
# Los Angeles
# Chicago5. Using Explicit Casting
Explicit casting in PowerShell is the fifth and final method for converting a variable to a string.
Explicit casting in PowerShell allows you to convert a variable to a string by specifying the [string] type.
Here is a complete example.
# Explicitly cast an integer to a string
$integerVariable = 456
$stringVariable = [string]$integerVariable
Write-Output $stringVariable # Output: 456
# Explicitly cast a boolean to a string
$booleanVariable = $true
$stringVariable = [string]$booleanVariable
Write-Output $stringVariable # Output: TrueLook at the screenshot below for the output of the above script after I executed it.

In this PowerShell tutorial, I have explained various methods that you can use to convert a variable to a string in PowerShell. Methods like:
- Using the ToString() Method
- Using String Interpolation
- Using the -f Format Operator
- Using Out-String Cmdlet
- Using Explicit Casting
The easiest method you can try is the ToString() method. We implemented all the methods with examples. Leave a comment below if you still have any questions.
You may also like:
- How to Split String by Tab Character in PowerShell?
- How to Compare Strings in PowerShell?
- Convert Strings to Lowercase or Uppercase in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.