Summary: Ed Wilson, Microsoft Scripting Guy, talks about using a new Windows PowerShell cmdlet to create objects from strings.
Hey, Scripting Guy! It seems that even though Windows PowerShell 5.0 in Windows 10 has a lot of cool new stuff, I still end up using command-line utilities. I don’t know if it is just me, or if there are still some things that are missing. I can get useful information from these command-line utilities, but I miss the experience of having an object that I can easily sort and filter. I guess what I am really wishing is that I had either more Windows PowerShell cmdlets or a way to convert text output into an object. I know you can’t really help me...just sayin'.
—BR
Hello BR,
Microsoft Scripting Guy, Ed Wilson, is here. In a few days we are heading to Orlando to meet up with Windows PowerShell MVP, Sean Kearney and his lovely bride. It will be cool (even if the weather in Orlando is still hot and sticky). Sean is always a lot of fun, and when we get together, we usually come up with some pretty cool things to do.
BR, you are in luck, because you are not the first person to wish for a better way of dealing with text output. This is why Microsoft Research created the way cool ConvertFrom-String cmdlet. In fact, it converts string data (text output) into an object. And it does it pretty much automatically. If this sounds too good to be true, it is truly an amazing cmdlet.
A long time ago, I wrote a script to convert IPCONFIG output into a series of objects. It was a pretty cool script, but it was nearly a dozen lines long. I don’t exactly remember how long it took me to write it, but it was at least several hours.
When I look at the output from IPCONFIG, I see a series of output that shows stuff on the left, numbers and things on the right, and a colon in the middle. Here is the output:
As shown here, when I look at the Help for ConvertFrom-String, I see that it wants a string for a delimiter:
PS C:\> help ConvertFrom-String
NAME
ConvertFrom-String
SYNOPSIS
Extracts and parses structured objects from string content.
SYNTAX
ConvertFrom-String [-Delimiter [<String>]] [-InformationAction
{SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}]
[-InformationVariable [<System.String>]] [-PropertyNames [<String[]>]]
-InputObject <String> [<CommonParameters>]
ConvertFrom-String [-InformationAction {SilentlyContinue | Stop | Continue |
Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]]
[-TemplateContent [<String>]] [-TemplateFile [<String>]] -InputObject <String>
[<CommonParameters>]
This means that I can supply a colon for the delimiter, and it should create objects:
IPCONFIG | ConvertFrom-String -Delimiter " : "
The output is excellent:
When I send the output to the Get-Member cmdlet, I can see that I have an object with a couple of custom properties:
PS C:\> IPCONFIG | ConvertFrom-String -Delimiter " : " | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
P1 NoteProperty string P1= Connection-specific DNS Suffix .
P2 NoteProperty string P2=
So I have an object. Cool. That means that I can filter out empty properties easily. This is shown here:
BR, that is all there is to using the ConvertFrom-String cmdlet. Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy