In this tutorial, I will explain how to convert a number to a string in PowerShell using various methods with examples.
PowerShell is built on the .NET framework, which means every piece of data has a specific type (e.g., Int32, Double, String). When you perform a conversion, you aren’t just changing the “look” of the data; you are changing how PowerShell treats it in the pipeline.
Numbers in PowerShell can be integers, doubles, decimals, or other numeric types, while strings are text-based data enclosed in quotes.
Now, let me show different methods to convert a number to a string in PowerShell.
Method 1: Using the ToString() Method
The most common and reliable way to convert numbers to strings in PowerShell is using the ToString() method. This method is available on all numeric types in PowerShell.
Basic ToString() Usage
Here are some basic uses of the ToString() method in PowerShell.
$number = 42
$stringValue = $number.ToString()
Write-Host "String value: $stringValue"
Write-Host "Type: $($stringValue.GetType().Name)"You can see the exact output in the screenshot below:

This approach works for all numeric types:
- Integers (int32, int64)
- Floating-point numbers (float, double)
- Decimals
- Byte values
ToString() with Format Strings
The ToString() method accepts format strings for precise control over output:
$price = 1234.56
$formatted = $price.ToString("C") # Currency format
Write-Host $formatted # Outputs: $1,234.56
$percentage = 0.85
$percentString = $percentage.ToString("P") # Percentage format
Write-Host $percentString # Outputs: 85.00%Common format specifiers:
- C – Currency format
- D – Decimal format (integers only)
- E – Exponential notation
- F – Fixed-point notation
- N – Number with thousand separators
- P – Percentage format
- X – Hexadecimal format
Here are some examples of the format specifiers above.
- Leading Zeros: Use
"D"followed by the number of digits.
(5).ToString("D3") # Output: 005- Currency: Use
"C"to apply the local culture’s currency symbol.
(1250).ToString("C") # Output: $1,250.00- Hexadecimal: Use
"X"to convert to hex.
(255).ToString("X") # Output: FFCheck out Find Dates in Strings with PowerShell
Method 2: String Interpolation
PowerShell’s string interpolation automatically converts numbers to strings when they’re embedded in double-quoted strings.
In PowerShell, double-quoted strings allow for “variable expansion.” This is an implicit conversion. When you place a numeric variable inside double quotes, PowerShell automatically calls its default .ToString() method.
$count = 100
$message = "There are $count items in the list"
Write-Host $messageYou can see the exact output in the screenshot below:

This method is convenient for quick conversions but offers less formatting control than ToString().
Here is another example:
$diskSpace = 50
$message = "You have $diskSpace GB remaining."
# PowerShell converts 50 to "50" automatically inside the string.Using Subexpressions for Complex Operations
For calculations within strings, use subexpressions with $() like below:
$x = 10
$y = 5
$result = "The sum of $x and $y is $($x + $y)"
Write-Host $result # Outputs: The sum of 10 and 5 is 15Check out PowerShell Convert Byte Array to Hex String
Method 3: Type Casting with [string]
PowerShell allows explicit type casting using type accelerators. This method is straightforward and readable.
$number = 789
$stringValue = [string]$number
Write-Host "Converted: $stringValue"Type casting works well for simple conversions but doesn’t provide formatting options like ToString() does.
When to Use Type Casting
Use type casting when:
- You need a quick, simple conversion
- No special formatting is required
- Code readability is a priority
- You’re working with pipeline operations
Read PowerShell New Line in String
Method 4: Using -f Format Operator
The format operator (-f) provides powerful string formatting capabilities similar to C# string formatting.
$amount = 1500.75
$formattedString = "Total: {0:C}" -f $amount
Write-Host $formattedString # Outputs: Total: $1,500.75Multiple Value Formatting
Format multiple numbers in a single operation:
$price = 49.99
$quantity = 3
$total = $price * $quantity
$invoice = "Item: {0:C} x {1} = {2:C}" -f $price, $quantity, $total
Write-Host $invoiceAdvantages of the format operator:
- Handle multiple values simultaneously
- Precise alignment and padding control
- Culture-specific formatting
- Consistent formatting across complex strings
Check out Convert String to Decimal in PowerShell
Best Practices for Number to String Conversion
Choose the right method:
- Use
ToString()for flexibility and control - Use string interpolation for simple, readable code
- Use format operator for complex multi-value scenarios
- Use type casting for explicit, clear conversions
Consider performance:
For bulk operations, ToString() is generally fastest. String interpolation adds slight overhead but improves readability.
Maintain consistency:
Choose one method for similar operations throughout your scripts to improve maintainability.
Document formatting choices:
When using custom formats, add comments explaining the purpose of the format string.
Here is a summary of different methods and usages.
| Method | Best For… | Example |
| .ToString() | Explicit conversion and simple padding. | $x.ToString("D2") |
| Type Casting | Quick variable assignments. | [string]$x |
| -f Operator | Complex strings and multiple variables. | '{0:C}' -f $x |
| Interpolation | Simple messages and log entries. | "Value: $x" |
Conclusion
In this tutorial, I explained how to convert a number to a string in PowerShell using different methods with examples.
The ToString() method offers the most flexibility, string interpolation provides simplicity, type casting ensures clarity, and the format operator delivers powerful multi-value formatting.
You may also like the following tutorials:
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.