Skip to content

Instantly share code, notes, and snippets.

@latkin latkin/readme.md
Last active Aug 29, 2015

Embed
What would you like to do?
Blog: A handy Powershell filter for converting plain text to objects
# converts text to objects via regex,
# with properties corresponding to capture groups
filter ro
{
param($pattern)
if($_ -match $pattern)
{
$result = @{}
$matches.Keys |?{ $_ } |%{
$raw = $matches[$_]
$asInt = 0
$asFloat = 0.0
$asDate = [datetime]::Now
if([int]::TryParse($raw, [ref] $asInt)){ $result[$_] = $asInt }
elseif([double]::TryParse($raw, [ref] $asFloat)){ $result[$_] = $asFloat }
elseif([datetime]::TryParse($raw, [ref] $asDate)){ $result[$_] = $asDate }
else{ $result[$_] = $raw }
}
[pscustomobject]$result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.