How to Convert Variables to Strings in PowerShell

To convert a variable to a string in PowerShell, cast it with [string], call its ToString() method, or put it inside double quotes. For a number, all three give the same text:

$orderId = 1042

$a = [string]$orderId
$b = $orderId.ToString()
$c = "$orderId"

"$a, $b, $c are all $($a.GetType().Name), $($b.GetType().Name), $($c.GetType().Name)"

Output:

1042, 1042, 1042 are all String, String, String

They don’t always agree, though. Dates, decimals, arrays and $null each behave a little differently depending on which one you use, and that’s what this guide covers.

I ran the examples in PowerShell 7.6 and Windows PowerShell 5.1 on a US-English Windows 11 PC, and both versions printed the same results.

Convert a number to a formatted string

ToString() accepts a format code, which is the easiest way to get currency, thousands separators or leading zeros. The -f operator does the same inside a longer message:

$price = 1249.56

$price.ToString('C')
$price.ToString('N0')
(7).ToString('D3')
(255).ToString('X2')
'{0} licenses at {1:C} each' -f 3, 19.99

Output:

$1,249.56
1,250
007
FF
3 licenses at $19.99 each
FormatMeaningExample result
CCurrency, using the current culture$1,249.56
N0, N2Number with separators and 0 or 2 decimals1,250
D3Whole number padded with zeros to 3 digits007
X2Hexadecimal, 2 digitsFF

Microsoft lists every code in Standard numeric format strings. For rounding before you format, see how to round to 2 decimal places.

Convert a date to a string

This is where the three methods split. Inside double quotes, PowerShell writes the date in a fixed format. ToString() follows the PC’s regional settings:

$date = Get-Date -Year 2026 -Month 9 -Day 28 -Hour 14 -Minute 30 -Second 0

"Inside quotes:   $date"
"ToString():      $($date.ToString())"
"Custom format:   $($date.ToString('MMMM d, yyyy h:mm tt'))"

Output:

Inside quotes:   09/28/2026 14:30:00
ToString():      9/28/2026 2:30:00 PM
Custom format:   September 28, 2026 2:30 PM
PowerShell convert a date variable to a string with quotes, ToString() and a custom format
Three ways to turn a date into text (PowerShell 7)

The first line is always month/day/year with a 24-hour clock, on any PC. When the text goes into a report or a file name, pass your own format to ToString() so you know exactly what you’ll get.

More patterns are in how to format dates in PowerShell.

Why does ToString() give a different result on another PC?

[string] and double quotes use the invariant culture, which never changes. ToString() uses the current culture. Here’s the same number on a PC set to German:

[System.Threading.Thread]::CurrentThread.CurrentCulture = 'de-DE'
$amount = 1234.5

"[string] cast:  $([string]$amount)"
"ToString():     $($amount.ToString())"
"Fixed culture:  $($amount.ToString([cultureinfo]::InvariantCulture))"

Output:

[string] cast:  1234.5
ToString():     1234,5
Fixed culture:  1234.5
PowerShell ToString() uses the current culture while a [string] cast does not
The same value with a German culture (Windows PowerShell 5.1)

Germany uses a comma for decimals, so ToString() wrote 1234,5. If a script builds CSV files or SQL values that another system reads, use the cast or pass InvariantCulture, and the output stays the same wherever the script runs.

How do I convert an array to a string?

Calling ToString() on an array returns its type name, not its contents. Casting joins the items with spaces, and -join lets you pick the separator:

$servers = 'WEB01', 'WEB02', 'SQL01'

"ToString():  $($servers.ToString())"
"[string]:    $([string]$servers)"
"-join:       $($servers -join ', ')"

Output:

ToString():  System.Object[]
[string]:    WEB01 WEB02 SQL01
-join:       WEB01, WEB02, SQL01
PowerShell convert an array variable to a string with ToString, [string] and -join
Converting an array to text (PowerShell 7)

I’d use -join every time. It says exactly what you want, and it doesn’t depend on the $OFS variable, which controls the separator for casts.

Out-String adds a line break

Out-String turns output into the text you’d see in the console. It also adds a line break at the end, which shows up when you put the result inside something else:

$text = 'WEB01', 'WEB02' | Out-String

"Length: $($text.Length)"
"[$text]"
"Trimmed: [$($text.Trim())]"

Output:

Length: 14
[WEB01
WEB02
]
Trimmed: [WEB01
WEB02]

Call .Trim() on the result when you only want the text.

Convert an object to a string

An object in double quotes becomes a hashtable-style summary. For something you can read back later, use JSON. For a readable table, use Out-String:

$user = [pscustomobject]@{ Name = 'Maria'; Department = 'Finance' }

"Inside quotes: $user"
$user | ConvertTo-Json -Compress
($user | Out-String).Trim()

Output:

Inside quotes: @{Name=Maria; Department=Finance}
{"Name":"Maria","Department":"Finance"}
Name  Department
----  ----------
Maria Finance

JSON is the best choice for logs and files other scripts will read. See PowerShell ConvertTo-Json for nested objects.

Converting $null to a string

A variable that’s $null can be cast safely, but calling a method on it throws an error:

$value = $null

"[string] cast: [$([string]$value)]"
try {
    $value.ToString()
}
catch {
    "ToString(): $($_.Exception.Message)"
}

Output:

[string] cast: []
ToString(): You cannot call a method on a null-valued expression.

When a value might be missing, like a property that isn’t always set, use the cast. You get an empty string instead of a stopped script.

Frequently Asked Questions

How do I convert a variable to a string in PowerShell?

Use [string]$value, $value.ToString() or "$value". For numbers they give the same result; for dates, arrays and $null they differ.

How do I convert a number to a string with commas?

Use $number.ToString('N0') for no decimals or ToString('N2') for two. '{0:N2}' -f $number does the same inside a longer string.

Why does ToString() show a comma instead of a decimal point?

ToString() follows the PC’s regional settings. Cast with [string] or pass [cultureinfo]::InvariantCulture to always get a decimal point.

How do I convert an array to a comma-separated string?

Use $array -join ', '. Calling $array.ToString() only returns System.Object[].

Why does Out-String add a blank line?

Out-String ends its text with a line break. Call .Trim() on the result to remove it.

Other conversion guides you may find useful: