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().fullnameSystem.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

  • Anonymous
    September 12, 2013
    Thanks for your input. I found this to be more reliable: if($_num -match "^[0-9]*$"){ "is int" }

  • Anonymous
    December 18, 2013
    @sci It should probably be "^[0-9]+$" to not match the empty string.

  • Anonymous
    May 16, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 16, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    May 17, 2014
    Pingback from Technical: Microsoft – Windows – Networking – Netstat – Code Snippet in Powershell | Daniel Adeniji's – Learning in the Open

  • Anonymous
    January 03, 2015
    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.

  • Anonymous
    January 09, 2015
    You may also want to consider using the following to match negative numbers:
    "^[-0-9]+$"

  • Anonymous
    June 09, 2015
    What about:
    ($String_or_Number -eq (($String_or_Number -as [double]) -as [string]))

  • Anonymous
    October 02, 2015
    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.