How to Check if a String Starts with a Number in PowerShell?

While working with string manipulations in PowerShell, I got a requirement to determine if a string starts with a number. In this tutorial, we will explore different methods to check if a string starts with a number in PowerShell with examples.

To check if a string starts with a number in PowerShell, you can use the -match operator with a regular expression. For example, consider the strings “123MainStreet” and “MainStreet789”. You can use the following code to determine if a string starts with a number:

$names = @("123MainStreet", "MainStreet789")
foreach ($name in $names) {
    if ($name -match '^\d') {
        Write-Output "$name starts with a number."
    } else {
        Write-Output "$name does not start with a number."
    }
}

This script uses the regex pattern ^\d to match any string that begins with a digit.

Check if a String Starts with a Number in PowerShell

Now, let me show you how to check if a string starts with a number in PowerShell using those methods.

Method 1: Using Regular Expressions

In the first method, let me show you how to use Regular expressions (regex) to check if a string starts with a number.

In PowerShell, you can use the -match operator with a regex pattern to check if a string starts with a number.

Let us understand with an example.

Example

Let us say, here are a few strings.

  • “123MainStreet”
  • “456ElmStreet”
  • “MainStreet789”
  • “7thAvenue”

We want to check if these strings start with a number using regular expressions.

$names = @("123MainStreet", "456ElmStreet", "MainStreet789", "7thAvenue")

foreach ($name in $names) {
    if ($name -match '^\d') {
        Write-Output "$name starts with a number."
    } else {
        Write-Output "$name does not start with a number."
    }
}
  • ^\d: The ^ asserts the position at the start of the string, and \d matches any digit.
  • The -match operator checks if the string matches the regex pattern.

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

powershell check if string starts with number

Read How to Substring in PowerShell?

Method 2: Using String Methods

PowerShell provides built-in string methods that can be used to check if a string starts with a number. The StartsWith method can be combined with the IsDigit method to check if the first character of the string is a digit in PowerShell.

Example

Here is an example.

$names = @("123MainStreet", "456ElmStreet", "MainStreet789", "7thAvenue")

foreach ($name in $names) {
    if ([char]::IsDigit($name[0])) {
        Write-Output "$name starts with a number."
    } else {
        Write-Output "$name does not start with a number."
    }
}
  • $name[0]: Accesses the first character of the string.
  • [char]::IsDigit: Checks if the character is a digit.

Look at the screenshot below to check the exact output of the above PowerShell script.

check if string starts with number powershell

Method 3: Using the Select-String Cmdlet

The Select-String cmdlet in PowerShell can be used to search for patterns in strings.

Let us check this with an example.

Example

Here is an example to check if a string starts with a number using the Select-String cmdlet in PowerShell.

$names = @("123MainStreet", "456ElmStreet", "MainStreet789", "7thAvenue")

foreach ($name in $names) {
    if ($name | Select-String -Pattern '^\d') {
        Write-Output "$name starts with a number."
    } else {
        Write-Output "$name does not start with a number."
    }
}
  • Select-String -Pattern '^\d': Searches for the pattern at the start of the string.

The screenshot below shows the output after I executed the above PowerShell script using VS code. You will also get the same output.

check if string starts with number in PowerShell

Conclusion

In PowerShell, you can check if a string starts with a number using various methods, such as regex, string methods, cmdlets, etc.

We saw a few examples of how to check if a string starts with a number in PowerShell, and I hope this helps.

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.