In a previous post I talked about parsing NetStat.exe using PowerShell and some regex, It is a fun exercice but require some knowledge to figure out how the parsing should happen.

Today, It got way easier ! The PowerShell Team just released a new version of the WMF : v5 September preview ! And One of the coolest feature is the new ConvertFrom-Stringcmdlet.

EDIT (2014/10/02): See also my post about using ConvertFrom-String and the param -TemplateFile against Netstat.exe

Using the same example from my previous post, I will perform a simple parsing of netstat.exe -n and send the output to ConvertFrom-String.

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.

Note: $netstat variable is holding the output of netstat.exe -n without the 5 first lines, you can refer to my previous post to see how to do this step.

Playing with ConvertFrom-String

Passing the Output of netstat to ConvertFrom-String, the cmdlet is parsing on the whitespace by default, but you can also specify it using the parameter -Delimiter

$netstat | ConvertFrom-String

The first property is empty since the output of netstat starts with a couple of whitespaces, the parsing interpret it as an empty property.

Adding some Properties

Next we can specify the Properties names to make it more explicit None is used for my first empty property called P1 in the previous example.

$netstat | ConvertFrom-String -PropertyNames None , Protocol , LocalIP , RemoteIP , Status

To remove the first empty property, we can modify the initial $netstat by removing the whitespace at the beginning of each line. Regex magic: ‘^\s+’

$netstat -replace '^\s+' | ConvertFrom-String -PropertyNames Protocol , LocalIP , RemoteIP , Status

That’s all for today, in a next post I will work with the new parameters TemplateFile and TemplateContent parameters.

Extra: Help Content

Current help content