ConvertTo-Object.ps1
<#PSScriptInfo .VERSION 1.0 .GUID dd5f3733-49ab-4da2-8bd3-c0e1c511ef33 .AUTHOR Mathias R. Jessen (@IISResetMe) .PROJECTURI https://gist.github.com/IISResetMe/654b302383a687bd92faa8c8c3ab28fa #> <# .DESCRIPTION Converts strings to objects using Regex .EXAMPLE logman query providers |.\ConvertTo-Object.ps1 -Pattern '^(?<Name>.+)\s+(?<GUID>\{\w+\})\s*$' In this example we parse the output from logman.exe #> param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [AllowEmptyString()] [string[]]$InputString, [Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)] [string[]]$Pattern ) process { foreach ($string in $InputString | ? { $_ }) { foreach ($p in $Pattern) { if ($string -match $p) { if ($PropertyNames = $Matches.Keys | Where-Object { $_ -is [string] }) { $Properties = $PropertyNames | ForEach-Object -Begin { $t = @{ } } -Process { $t[$_] = $Matches[$_] } -End { $t } [PSCustomObject]$Properties } break } } } } |