Skip to content

Instantly share code, notes, and snippets.

@latkin
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latkin/1b849d8832babf38e4e9 to your computer and use it in GitHub Desktop.
Save latkin/1b849d8832babf38e4e9 to your computer and use it in GitHub Desktop.
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