How to Convert String to Int in PowerShell?

Recently, one of my team members struggled to figure out how to convert a string to an int in a PowerShell automation script. I suggested a few methods. In this tutorial, I will show you how to convert string to int in PowerShell using six methods with examples.

To convert a string to an integer in PowerShell, you can use the [int] type accelerator. For example, if you have a string $stringNumber = “123”, you can convert it to an integer by casting it: $intNumber = [int]$stringNumber. This approach is straightforward and effectively converts the string representation of a number to an integer.

Convert a String to an Integer in PowerShell

Let me show you different methods to convert a string to an int in PowerShell. We will take examples for each method. I have executed all the scripts using the VS code editor, but you can use any editor you prefer.

We will cover the following methods in detail:

  1. Using Type Casting
  2. Using the [int] Type Accelerator
  3. Using the ::Parse() Method
  4. Using the ::TryParse() Method
  5. Using the Convert Class
  6. Using -as Operator

Method 1: Using Type Casting

Type casting is one of PowerShell’s simplest ways to convert a string to an integer. You can cast a string to an integer by prefixing the string with [int].

Example:

$stringNumber = "123"
$integerNumber = [int]$stringNumber
Write-Output $integerNumber

In this example, the string "123" is cast to an integer using [int]. The result is stored in $integerNumber, which is then output to the console.

You can see the output in the screenshot below, after I executed the above PowerShell script.

Convert String to Int in PowerShell

This method works well for simple conversions. However, it has limitations:

  • It throws an exception if the string cannot be converted to an integer.
  • It doesn’t handle whitespace or non-numeric characters well.

Casting with [int] is mostly used when you’re certain that the string contains a valid integer value.

Handle Non-Numeric Strings:

If the string contains non-numeric characters, PowerShell will throw an error. You can handle the error by using a try-catch block like the one below:

$stringNumber = "123abc"
try {
    $integerNumber = [int]$stringNumber
} catch {
    Write-Output "Error: Cannot convert '$stringNumber' to an integer."
}

Let me show you another real example that most PowerShell developers use.

Suppose you have a script that prompts the user for two numbers and then sums them.

Here is how you can write the complete PowerShell script.

$firstNumber = Read-Host "Enter the first number"
$secondNumber = Read-Host "Enter the second number"

try {
    $sum = [int]$firstNumber + [int]$secondNumber
    Write-Output "The sum is: $sum"
} catch {
    Write-Output "Error: Please enter valid numbers."
}

In this script, user input is read as strings and then converted to integers using [int] before performing the addition.

Here, you can see in the screenshot below it is throwing an exception as I did not enter a number for the 2nd option.

powershell convert string to int

Read Convert Strings to Lowercase or Uppercase in PowerShell

2. Using the [int] Type Accelerator

The [int] type accelerator is a shorthand for casting a string to an integer in PowerShell. It works similarly to the type-casting method.

Example:

$stringNumber = "456"
$integerNumber = [int]$stringNumber
Write-Output $integerNumber

This is another simple method to convert a string to a number in PowerShell.

3. Using the ::Parse() Method

The ::Parse() method is part of the .NET framework that is used to convert strings to integers in PowerShell. This method is strict and throws an exception if the string cannot be parsed as an integer. It’s useful when you want to ensure that the input is a valid integer and handle exceptions explicitly.

Example:

$stringNumber = "789"
$integerNumber = [int]::Parse($stringNumber)
Write-Output $integerNumber

When you execute the above script, it will return 789.

Handle Non-Numeric Strings:

To handle non-numeric strings, you can use a try-catch block like below.

$stringNumber = "789xyz"
try {
    $integerNumber = [int]::Parse($stringNumber)
} catch {
    Write-Output "Error: Cannot parse '$stringNumber' as an integer."
}

The output is shown in the screenshot below. I executed the above script, and it threw an exception.

powershell convert string to int error handling

Read Convert String to JSON in PowerShell

4. Using the ::TryParse() Method

The ::TryParse() method is a safer alternative to ::Parse() in PowerShell. It attempts to convert the string to an integer and returns a boolean indicating success or failure. It does not throw an exception if the conversion fails.

Example:

$stringNumber = "42"
$intNumber = 0
if ([int]::TryParse($stringNumber, [ref]$intNumber)) {
    Write-Output "Conversion successful: $intNumber"
} else {
    Write-Output "Conversion failed"
}

In this example, ::TryParse() attempts to convert the string "42" to an integer. If successful, the integer value is stored in $integerNumber, and the result is output to the console. Otherwise, an error message is displayed.

Read Count the Number of Characters in a String in PowerShell

5. Using the Convert Class

The Convert class in PowerShell provides a method ToInt32() to convert strings to integers. This method is part of the .NET framework and is similar to the ::Parse() method.

Example:

$stringNumber = "131415"
$integerNumber = [Convert]::ToInt32($stringNumber)
Write-Output $integerNumber

Handle Non-Numeric Strings:

To handle non-numeric strings, use a try-catch block like the one below.

$stringNumber = "131415abc"
try {
    $integerNumber = [Convert]::ToInt32($stringNumber)
} catch {
    Write-Output "Error: Cannot convert '$stringNumber' to an integer."
}

Look at the screenshot below for the exact output after I executed the above PowerShell script.

powershell convert string to int error

6. Using -as Operator

The -as operator is another way to perform type conversions in PowerShell. It attempts to convert the value to the specified type and returns $null if the conversion fails.

Example:

$stringNumber = "101"
$intNumber = $stringNumber -as [int]
if ($intNumber -ne $null) {
    Write-Output "Conversion succeeded: $intNumber"
} else {
    Write-Output "Conversion failed"
}

In this case, the -as operator converts the string "101" to the integer 101. If the string cannot be converted, $null is returned.

Conclusion

As a developer in PowerShell, you will encounter the requirement to convert a string to an integer. In this tutorial, I have explained how to convert a string to an integer in PowerShell using various methods with examples.

I prefer to use the type casting method for converting strings to integers in PowerShell.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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