Home ยป How to Convert String to Integer in PowerShell

How to Convert String to Integer in PowerShell

In PowerShell, there are several ways to convert string to integer in PowerShell.

The following methods show how you can do it with syntax.

Method 1: Using Convert.ToInt32() method

$intNumber = [System.Convert]::ToInt32($stringNumber)

This example will parse the string and convert it to an integer.

Method 2: Using int.TryParse() method

[int]::TryParse($stringNumber,[ref]$intValue)

This example will convert the string to an integer.

Method 3: Using Casting

$intNumber = [int]$stringNumber

This example will convert the string to an integer using casting.

Method 4: Using Parse() method

$intNumber = [int]::Parse($stringNumber)

The following examples show how you can use each of the methods.

Convert String to Integer Using Convert.ToInt32() method

You can use the ToInt32() method to convert the string to an integer.

# define a string 
$stringNumber = "293"

# convert string to integer using ToInt32()
$intNumber = [System.Convert]::ToInt32($stringNumber)
Write-Output $intNumber

Output:

293

In this script, we defined a string variable $stringNumber that contains a number in string type.

To convert the string to an integer, we used the ToInt32() method and passed the $stringNumber as an input parameter. It returns the integer representation of string variable $stringNumber.

Finally, we use the Write-Output cmdlet to print the number to the console.

Convert String to Integer Using TryParse() Method

A safe way to convert a string to an integer is to use the [int]::TryParse() method in PowerShell. It attempts to convert the string and returns the true if the conversion succeeds or false if it fails.

# define a string representing integer
$stringNumber = "20"
$intValue = 0
if([int]::TryParse($stringNumber,[ref]$intValue)){
    Write-Output "Integer value is: $intValue"
} else {
   Write-Output "Conversion failed"
} 

Output:

20

In this script, the $stringNumber variable contains the string representing an integer. We used the TryParse() method to convert the string to an integer.

Finally, if the conversion succeeds, it prints the output using the Write-Output cmdlet.

Convert String to Integer Using Casting

The easy way to convert a string to an integer is to use casting.

# define a string representing integer
$stringNumber = "15"
$intNumber = [int]$stringNumber
Write-Output $intNumber

Output:

15

In this script, we used the [int] casting to convert a string to an integer value.

Finally, it prints the integer value using the Write-Output cmdlet in PowerShell.

Convert String to an Integer Using Parse() Method

Another way to convert a string to an integer value is to use the Parse() method in PowerShell.

# define a string representing integer
$stringNumber = "20"
$intNumber = [int]::Parse($stringNumber)

Write-Output $intNumber

Output:

20

In this script, the $stringNumber variable contains the string representing an integer. We then use [int]::Parse() method which takes $stringNumber as an argument to convert a string to an integer value.

Finally, we use the Write-Output cmdlet to print the integer value.

Conclusion

I hope the above article on converting a string to an integer in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.