Q:

Extract number from a variable in powershell

I am very new to powershell and am trying to extract a number from a variable.  For instance, I get the list of ports using this command:
$ports = [system.io.ports.serialport]::getportnames()
The contents of $ports is: COM1, COM2, COM10, COM16 and so on. 
I want to extract the numbers from $ports.  I looked at this question.  It does what I want, but reads from a file. 
Please let me know how to resolve this. 
Thanks. 
Edit: I was able to do what I wanted as follows:
 $port=COM20

 $port=$port.replace("COM","")

But if there is any other way to do this, I will be happy to learn it. 

A:

Well, a quick way would be
$portlist = [System. IO.Ports. SerialPort]::GetPortNames() -replace 'COM'

If you want it to be a list of integers and not numeric strings, then you can use
[int[]] $portlist = ...

