{"id":7101,"date":"2019-12-18T10:06:44","date_gmt":"2019-12-18T15:06:44","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=7101"},"modified":"2019-12-18T10:06:51","modified_gmt":"2019-12-18T15:06:51","slug":"converting-lexical-timespans-with-powershell","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","title":{"rendered":"Converting Lexical Timespans with PowerShell"},"content":{"rendered":"<p>I've been working on a few scripting projects and the data I'm working with contains lexical timespans. Say what? You have probably seen these things. This is a string like P0DT0H0M47S to represents a timespan. They aren't difficult for humans to read. This one says \"0 days 0 hours 0 minutes 47 seconds\". The format is how timespans, or durations, are stored in XML files. You can read more about this at <a href=\"https:\/\/www.w3.org\/TR\/xmlschema-2\/#duration\">https:\/\/www.w3.org\/TR\/xmlschema-2\/#duration<\/a>. But I want to see these value as more PowerShell-friendly timespans. Turns out, this is actually very easy.<\/p>\n<p><!--more--><\/p>\n<p>One place you can see these values is with scheduled tasks in Windows.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-19.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"A lexical duration\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png\" alt=\"A lexical duration\" width=\"1028\" height=\"180\" border=\"0\" \/><\/a><\/p>\n<p>I know PT72H represents a 72 hour timespan. To convert this into a timespan object is simple. Once you know how and in a second you will. You can use a built-in XML method to convert the value.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">[System.Xml.XmlConvert]::ToTimeSpan(\"PT72H\")\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-20.png\"><img loading=\"lazy\" decoding=\"async\" style=\"margin: 0px; display: inline; background-image: none;\" title=\"image\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-20.png\" alt=\"image\" width=\"959\" height=\"541\" border=\"0\" \/><\/a><\/p>\n<p>And sure enough, this is 72 hours or 3 days. What about converting the other way?<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$ts = New-TimeSpan -hours 72\n[System.Xml.XmlConvert]::ToString($ts)\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-21.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Converting a timespan\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-21.png\" alt=\"Converting a timespan\" width=\"779\" height=\"175\" border=\"0\" \/><\/a><\/p>\n<p>The string is admittedly different, but it still represents 72 hours.<\/p>\n<p>The process is simple, but I still wrote a pair of PowerShell advanced functions to handle these tasks.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Function ConvertTo-LexicalTimespan {\n    [cmdletbinding()]\n    [OutputType(\"String\")]\n    Param(\n        [Parameter(Position = 0, Mandatory,HelpMessage = \"Enter a timespan object\", ValueFromPipeline)]\n        [ValidateNotNullOrEmpty()]\n        [timespan]$Timespan\n    )\n    Begin {\n        Write-Verbose \"[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)\"\n\n    } #begin\n\n    Process {\n        Write-Verbose \"[$((Get-Date).TimeofDay) PROCESS] Converting $Timespan\"\n        Try {\n            [System.Xml.XmlConvert]::ToString($Timespan)\n        }\n        Catch {\n            Throw $_\n        }\n    } #process\n\n    End {\n        Write-Verbose \"[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)\"\n    } #end\n\n} #close ConvertTo-LexicalTimespan\n\nFunction ConvertFrom-LexicalTimespan {\n    [cmdletbinding()]\n    [OutputType(\"string\", \"timespan\")]\n    Param(\n        [Parameter(Position = 0, Mandatory, HelpMessage = \"Enter a lexical time string like P23DT3H43M. This is case-sensitive.\", ValueFromPipeline)]\n        [ValidateNotNullOrEmpty()]\n        [string]$String,\n        [Parameter(HelpMessage = \"Format the timespan as a string\")]\n        [switch]$AsString\n    )\n    Begin {\n        Write-Verbose \"[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)\"\n    } #begin\n\n    Process {\n        Write-Verbose \"[$((Get-Date).TimeofDay) PROCESS] Converting $($String.toUpper()) to a timespan\"\n        Try {\n            #convert string to upper case to help the user out\n            $r = [System.Xml.XmlConvert]::ToTimeSpan($String.ToUpper())\n            if ($AsString) {\n                Write-Verbose \"[$((Get-Date).TimeofDay) PROCESS] Displaying as a timespan string\"\n                $r.toString()\n            }\n            else {\n                $r\n            }\n        }\n        Catch {\n            Throw $_\n        }\n    } #process\n\n    End {\n        Write-Verbose \"[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)\"\n    } #end\n\n} #close ConvertFrom-LexicalTimespan\n<\/pre>\n<p>Let's see how these work.<\/p>\n<p>Here's another raw example.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-22.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Lexical durations\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-22.png\" alt=\"Lexical durations\" width=\"1028\" height=\"316\" border=\"0\" \/><\/a><\/p>\n<p>Or I can use code like this:<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">(get-scheduledtask dailywatcher).settings.idlesettings | \nselect-object @{Name=\"Idle\";Expression={ConvertFrom-LexicalTimespan $_.idleDuration}},\n@{Name=\"Wait\";Expression={ConvertFrom-LexicalTimespan $_.WaitTimeout}}\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-23.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Converted lexical durations\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-23.png\" alt=\"Converted lexical durations\" width=\"1028\" height=\"218\" border=\"0\" \/><\/a><\/p>\n<p>Now, I have more meaningful output.<\/p>\n<p>Here's one final example using <a title=\"Read online help for this command\" href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=141444\" target=\"_blank\" rel=\"noopener noreferrer\">Get-WsManInstance<\/a> to show connected pssessions.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-24.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Resule with lexical durations\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-24.png\" alt=\"Result with lexcial durations\" width=\"1028\" height=\"267\" border=\"0\" \/><\/a><\/p>\n<p>But I think you'll agree that using this code produces a better result.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Get-WSManInstance -ComputerName DOM1 -ResourceURI Shell -Enumerate | \n Select-Object -Property ResourceURI,Owner,ClientIP,\n @{Name=\"ShellRunTime\";Expression = {ConvertFrom-LexicalTimespan $_.ShellRunTime}},\n @{Name=\"ShellInactivity\";Expression = {ConvertFrom-LexicalTimespan $_.ShellInactivity}},\n @{Name=\"IdleTimeOut\";Expression = {ConvertFrom-LexicalTimespan $_.IdleTimeOut}},\n @{Name=\"MaxIdleTimeOut\";Expression = {ConvertFrom-LexicalTimespan $_.MaxIdleTimeOut}},\n @{Name=\"Computername\";Expression={\"DOM1\"}}\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-25.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Properly formatted timespans\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-25.png\" alt=\"Properly formatted timespans\" width=\"1028\" height=\"275\" border=\"0\" \/><\/a><\/p>\n<p>This is code I would put into a function to create meaningful output to the PowerShell pipeline. Or if I wanted something more interactive, I could use my <a title=\"check out the module's repository on GitHub to learn more\" href=\"https:\/\/github.com\/jdhitsolutions\/PSTypeExtensionTools\" target=\"_blank\" rel=\"noopener noreferrer\">PSTypeExtensionTools<\/a> module and create extensions.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t = \"System.Xml.XmlElement#http:\/\/schemas.microsoft.com\/wbem\/wsman\/1\/windows\/shell#Shell\"\n$t | Add-PSTypeExtension -MemberType ScriptProperty -MemberName ShellRun -Value {[System.Xml.XmlConvert]::ToTimeSpan($this.ShellRunTime) }\n$t | Add-PSTypeExtension -MemberType ScriptProperty -MemberName ShellInactive -Value {[System.Xml.XmlConvert]::ToTimeSpan($this.ShellInactivity) }\n$t | Add-PSTypeExtension -MemberType ScriptProperty -MemberName Idle -Value {[System.Xml.XmlConvert]::ToTimeSpan($this.IdleTimeout) }\n$t | Add-PSTypeExtension -MemberType ScriptProperty -MemberName MaxIdle -Value {[System.Xml.XmlConvert]::ToTimeSpan($this.MaxIdleTimeout) }\n<\/pre>\n<p>This makes it easier to run the command and select the new properties.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image-26.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Using PowerShell type extensions\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-26.png\" alt=\"Using PowerShell type extensions\" width=\"1028\" height=\"417\" border=\"0\" \/><\/a><\/p>\n<p>Or I could create a custom format view. But I'll leave that exercise for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a few scripting projects and the data I&#8217;m working with contains lexical timespans. Say what? You have probably seen these things. This is a string like P0DT0H0M47S to represents a timespan. They aren&#8217;t difficult for humans to read. This one says &#8220;0 days 0 hours 0 minutes 47 seconds&#8221;. The format&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Now on the blog: Converting Lexical Timespans with #PowerShell","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[534,126,206],"class_list":["post-7101","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-powershell","tag-timespan","tag-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-18T15:06:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-18T15:06:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Converting Lexical Timespans with PowerShell\",\"datePublished\":\"2019-12-18T15:06:44+00:00\",\"dateModified\":\"2019-12-18T15:06:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/\"},\"wordCount\":309,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/image_thumb-19.png\",\"keywords\":[\"PowerShell\",\"TimeSpan\",\"xml\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/\",\"name\":\"Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/image_thumb-19.png\",\"datePublished\":\"2019-12-18T15:06:44+00:00\",\"dateModified\":\"2019-12-18T15:06:51+00:00\",\"description\":\"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/image_thumb-19.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/image_thumb-19.png\",\"width\":1028,\"height\":180},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7101\\\/converting-lexical-timespans-with-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting Lexical Timespans with PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator","description":"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator","og_description":"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","og_site_name":"The Lonely Administrator","article_published_time":"2019-12-18T15:06:44+00:00","article_modified_time":"2019-12-18T15:06:51+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Converting Lexical Timespans with PowerShell","datePublished":"2019-12-18T15:06:44+00:00","dateModified":"2019-12-18T15:06:51+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/"},"wordCount":309,"commentCount":1,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png","keywords":["PowerShell","TimeSpan","xml"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","name":"Converting Lexical Timespans with PowerShell &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png","datePublished":"2019-12-18T15:06:44+00:00","dateModified":"2019-12-18T15:06:51+00:00","description":"Let me show you how and why you might want to convert a lexical duration like P0DT1H6M30S into a PowerShell friendly timespan object.,","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-19.png","width":1028,"height":180},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Converting Lexical Timespans with PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":4414,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4414\/converting-timespans-to-repetition-patterns\/","url_meta":{"origin":7101,"position":0},"title":"Converting Timespans to Repetition Patterns","author":"Jeffery Hicks","date":"May 26, 2015","format":false,"excerpt":"Over on Petri.com, I've recently published a followup article about creating daily or weekly scheduled PowerShell jobs that support a repetition interval. The short answer is to use the Scheduled Tasks cmdlets. In the Petri article I talk about needing to use a special string format for the timespan. It\u2026","rel":"","context":"In &quot;Petri IT Knowledgebase&quot;","block_context":{"text":"Petri IT Knowledgebase","link":"https:\/\/jdhitsolutions.com\/blog\/category\/petri-it-knowledgebase\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7565,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/","url_meta":{"origin":7101,"position":1},"title":"Formatting PowerShell TimeSpans","author":"Jeffery Hicks","date":"June 24, 2020","format":false,"excerpt":"I often will figure out how to do something and later struggle to remember how to do it a months later. Rather than trying to remember what piece of code I wrote,\u00a0 why not write about. Assuming I can remember! Anyway, here's today's \"PSRemembery\". I often\u00a0 use code like this,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":8357,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8357\/friday-fun-counting-down-events-with-powershell\/","url_meta":{"origin":7101,"position":2},"title":"Friday Fun: Counting Down Events with PowerShell","author":"Jeffery Hicks","date":"April 30, 2021","format":false,"excerpt":"We just finished a very successful virtual edition of the PowerShell+DevOps Global Summit. We lost our 2020 event to the pandemic but fortunately, the people at The DevOps Collective were able to pull together a fantastic virtual event. There were as many virtual attendees as we normally have at the\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/get-pscountdown-format2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/get-pscountdown-format2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/get-pscountdown-format2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/get-pscountdown-format2.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":7956,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7956\/friday-fun-a-powershell-christmas-prompt\/","url_meta":{"origin":7101,"position":3},"title":"Friday Fun &#8211; A PowerShell Christmas Prompt","author":"Jeffery Hicks","date":"December 11, 2020","format":false,"excerpt":"Time for a new Friday Fun article. These articles use \"fun\" ways to teach you how to use PowerShell. The end goal may be silly, but hopefully the techniques and concepts are useful. Today I have an updated prompt function. You can customize your PowerShell prompt by creating a function\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/christmas-countdown.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7447,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7447\/get-installed-powershell-versions\/","url_meta":{"origin":7101,"position":4},"title":"Get Installed PowerShell Versions","author":"Jeffery Hicks","date":"May 7, 2020","format":false,"excerpt":"As is the norm for a typical day, I was working on one thing when I was distracted by a shiny rabbit hole (to mix some metaphors). Half a day later I have a new PowerShell function that not only might you find useful, but I think it has some\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":478,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/478\/is-it-friday-yet\/","url_meta":{"origin":7101,"position":5},"title":"Is It Friday Yet?","author":"Jeffery Hicks","date":"November 5, 2009","format":false,"excerpt":"Some days the week just seems to drag on and on and on\u2026.Like many of you, I can\u2019t wait for the end of the work day on Friday afternoon. But how much longer is it until I can say, in a cliche ridden fashion, \u201cTGIF!\u201d Fortunately I have PowerShell to\u2026","rel":"","context":"In &quot;Miscellaneous&quot;","block_context":{"text":"Miscellaneous","link":"https:\/\/jdhitsolutions.com\/blog\/category\/miscellaneous\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7101"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7101\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}