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\dmatches any digit.- The
-matchoperator 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:

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.

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.

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:
- Convert Strings to Lowercase or Uppercase in PowerShell
- Count the Number of Characters in a String in PowerShell
- Check if a String Contains Multiple Values in PowerShell
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.