How to Check if a String Contains Only Numbers in PowerShell?

One of my team members was searching for a PowerShell script to check if a string contained only numeric characters. I suggested a few methods. In this tutorial, I will explain how to check if a string contains only numbers in PowerShell using various methods.

To check if a string contains only numbers in PowerShell, you can use the -match operator with a regular expression. For example, to validate a ZIP code, you can use the following code:

$zipCode = "12345"
if ($zipCode -match '^\d+$') {
    Write-Output "The string contains only numbers."
} else {
    Write-Output "The string contains non-numeric characters."
}

Here, the regex pattern ^\d+$ ensures that the string consists only of digits from start to end.

Let me show you all the methods with examples:

Method 1: Using Regular Expressions

In PowerShell, you can use Regular expressions (regex) to match patterns within strings. In PowerShell, you can use the -match operator to determine if a string contains only numeric characters.

Let us understand with an example.

Example

Let’s say you have a string representing a ZIP code, which should only contain numbers:

$zipCode = "12345"

if ($zipCode -match '^\d+$') {
    Write-Output "The string contains only numbers."
} else {
    Write-Output "The string contains non-numeric characters."
}

In this example, the regex pattern ^\d+$ checks if the string contains only digits from start (^) to end ($). If the string matches this pattern, it means it contains only numbers.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below.

powershell check if string contains only numbers

Read PowerShell IsNullOrEmpty() Example

Method 2: Using the -replace Operator

Let me show you another method to check if the string contains only numbers in PowerShell.

You can use the -replace operator to remove all numeric characters from the string and then check if the resulting string is empty.

Let me show you an example with the complete script.

Example

Consider a string representing a Social Security Number (SSN):

$ssn = "987654321"

if (($ssn -replace '\d', '') -eq '') {
    Write-Output "The string contains only numbers."
} else {
    Write-Output "The string contains non-numeric characters."
}

Here, the -replace '\d', '' part removes all digits from the string. If the resulting string is empty (-eq ''), it means the original string contained only numbers.

You can see the screenshot for the actual output after I executed the above PowerShell script.

check if string contains only numbers powershell

Check out Check if a String Contains a Space in PowerShell

Method 3: Using the Select-String Cmdlet

In PowerShell, you can also use the Select-String cmdlet to search for patterns within strings. This method is particularly useful when working with files or larger datasets.

Example

Suppose you have a list of employee IDs stored in a text file, and you want to ensure they all contain only numbers.

Here is the complete PowerShell script.

$employeeIDs = Get-Content -Path "C:\Bijay\employeeIDs.txt"

foreach ($id in $employeeIDs) {
    if ($id -match '^\d+$') {
        Write-Output "$id is valid (contains only numbers)."
    } else {
        Write-Output "$id is invalid (contains non-numeric characters)."
    }
}

Here, Get-Content reads the file line by line, and the -match '^\d+$' pattern checks each line to ensure it contains only numeric characters.

Read PowerShell: IsNullOrEmpty vs IsNullOrWhiteSpace

Method 4: Using the TryParse Method

If you want to try a .Net method, then you can use a .Net oriented approach.

You can use the [int]::TryParse method to validate if a string can be parsed as an integer.

Example

Here is a string representing a phone number without any formatting:

$phoneNumber = "2025550198"
$result = [int]::TryParse($phoneNumber, [ref]$null)

if ($result) {
    Write-Output "The string contains only numbers."
} else {
    Write-Output "The string contains non-numeric characters."
}

In this example, [int]::TryParse will parse the string as an integer. If successful, it returns $true, indicating the string contains only numeric characters.

If you will execute the above script, then you can see the output in the screenshot below:

Check if a String Contains Only Numbers in PowerShell

Conclusion

In this tutorial, I have shown you how to check if a string contains only numbers in PowerShell using various methods like:

  • Using Regular Expressions
  • Using the -replace Operator
  • Using the Select-String Cmdlet
  • Using the TryParse Method

I hope these examples will help you to learn this.

You may also 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.