Something like this should work:
# initialize the variable that we want to use to store the port numbers.
$portList = @()
# foreach object returned by GetPortNames...
[IO.Ports.SerialPort]::GetPortNames() | %{
# replace the pattern "COM" at the beginning of the string with an empty
# string, just leaving the number. Then add the number to our array.
$portList += ($_ -ireplace "^COM", [String]::Empty)
}
Note that I used [IO.Ports.SerialPort] instead of [System.IO.Ports.SerialPort]. These are the same things - PowerShell implicitly assumes that you're working with the [System] namespace, so you don't need to specify it explicitly, though there's nothing wrong with doing it.
Edit
To answer your questions:
%{...} is shorthand for foreach-object {...}.
$_ indicates the object that is currently in the pipeline. When we're inside a foreach-object block, $_ resolves to the one object out of the entire collection that we're currently dealing with.
If we write my code a bit differently, I think it'll be easier to understand. Between these examples, $_ and $port are the same things.
$portList = @()
foreach ($port in [IO.Ports.SerialPorts]::GetPortNames()) {
$portList += ($port -ireplace "^COM", [String]::Empty)
}
Hope that helps!