How can I tell if a string in PowerShell contains a number?


I came across a scenario where I wanted to handle data that contained a number in the string one way and everything else a different way.  Using the Get-type() didn’t work for this case because the variable was handles as a string.

PS C:\> $test = "abc123"
PS C:\> $test.GetType().fullname
System.String 

I saw that there were some people on the intertubes that worked through this by splitting the string into an array and getting the ASCII value for each character and checking their value to see if they were in the range.  Something like this;

$Check = "abc123"
$Sort = [int[]][char[]]$Check

foreach ($Value in $Sort)
{
     if ( (($Value -ge 65) -and  ($Value -le 90)) -or (($Value -ge 97) -and  ($Value -le 122)) )
         {
                 write-host "Letter"
    }
}

 

 

The way I worked out was to check the string against a basic regex pattern to see if any of it matched a number 0-9;

$Check = "abc123"
if($Check -match "[0-9]")

{
    write-host "Number"
}

Comments (21)
  1. sci says:

    Thanks for your input. I found this to be more reliable:

    if($_num -match "^[0-9]*$"){

    "is int"

    }

  2. Jason R. Coombs says:

    @sci It should probably be “^[0-9]+$” to not match the empty string.

  3. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  4. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  5. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  6. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  7. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  8. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  9. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  10. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  11. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  12. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  13. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  14. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  15. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  16. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  17. Anonymous says:

    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji’s – Learning in the Open

  18. badhonq1w2 says:

    Thank you for your blog.I like your article very much.It is very helpful to my website.Any one can visit my

    http://www.blueparkmusic.com/">Benjamin Gabriel Parker site.

  19. Morlee says:

    You may also want to consider using the following to match negative numbers:
    "^[-0-9]+$"

  20. crssi says:

    What about:
    ($String_or_Number -eq (($String_or_Number -as [double]) -as [string]))

  21. Stefan says:

    If you evaluate your above examples with $_num = "random string 12" it will fail. If you use -match "[0-9]" it will be correct, while examples in the comments will fail.

Comments are closed.

Skip to main content