How to Convert Variables to Strings in PowerShell?

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 $stringDate

I executed the PowerShell script, and you can see the output in the screenshot below.

Convert Variables to Strings in PowerShell

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 $stringVariable

The screenshot below shows the output; you will also get the same output when you run the above script.

convert a variable to a string in PowerShell

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
# Chicago

5. 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: True

Look at the screenshot below for the output of the above script after I executed it.

PowerShell convert variable to string

In this PowerShell tutorial, I have explained various methods that you can use to convert a variable to a string in PowerShell. Methods like:

  1. Using the ToString() Method
  2. Using String Interpolation
  3. Using the -f Format Operator
  4. Using Out-String Cmdlet
  5. 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.