{"id":7565,"date":"2020-06-24T10:31:40","date_gmt":"2020-06-24T14:31:40","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=7565"},"modified":"2020-09-30T17:17:12","modified_gmt":"2020-09-30T21:17:12","slug":"formatting-powershell-timespans","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/","title":{"rendered":"Formatting PowerShell TimeSpans"},"content":{"rendered":"\n<p>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,&nbsp; why not write about. Assuming I can remember! Anyway, here's today's \"PSRemembery\".<\/p>\n\n\n\n<p>I often&nbsp; use code like this, and I expect many of you do as well.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">$os = Get-CimInstance win32_operatingsystem\n$os | Select-Object CSName,LastBootUpTime,\n@{Name=\"Uptime\";Expression={(Get-Date) - $_.lastbootuptime}}\n<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7566\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png\" alt=\"uptime\" width=\"925\" height=\"179\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png 925w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime-300x58.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime-768x149.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime-850x164.png 850w\" sizes=\"auto, (max-width: 925px) 100vw, 925px\" \/>This is pretty straight forward. Subtract the LastBootUpTime property from the current datetime to get a timespan object that shows how long this computer has been up and running. But...I want to get rid of the milliseconds value.&nbsp; It's irrelevant as far as I'm concerned and takes up space that I might want to use for another property.&nbsp; I need to format that&nbsp; value.<\/p>\n\n\n\n<p>I'll create a variable with this timespan value.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">$up = (Get-Date) - $os.lastbootuptime<\/code><\/pre>\n\n\n\n<p>As you can see, the timespan object has a ToString() method which PowerShell is using when it formats the result for me.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"738\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-1.png\" alt=\"timespan-1\" class=\"wp-image-7567\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-1.png 637w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-1-259x300.png 259w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-1-300x348.png 300w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/figure>\n\n\n\n<p>It turns out I can use this method to format the string to meet my needs. But it is tricky and this is the part I always forget. You can use case-sensitive tokens.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"110\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-2.png\" alt=\"timespan-2\" class=\"wp-image-7568\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-2.png 535w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-2-300x62.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/figure>\n\n\n\n<p>This gets rid of the millesecond value but it is a bit hard to decipher. I can add other bits of text to the format string but I have to tell PowerShell to treat them as literal strings.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">$up.ToString(\"dd\\.hh\\:mm\\:ss\")<\/code><\/pre>\n\n\n\n<p>I am inserting the puncuation to make the timespan easier to read.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"213\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-3.png\" alt=\"timespan-3\" class=\"wp-image-7569\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-3.png 749w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-3-300x85.png 300w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/figure>\n\n\n\n<p>If I wanted to include say the milliseconds to 2 decimal points I could do that as well. Let's take this a step further. I know some people like to see the string that includes 'days', 'hours', and 'minutes' (or some variation) as part of the output. I can do that as well. Here are a few examples.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">$up.ToString(\"dd'dy:'hh'hr:'mm'min:'ss'sec'\")\n$up.ToString(\"dd' days 'hh' hours 'mm' minutes 'ss' seconds'\")<\/code><\/pre>\n\n\n\n<p>Instead of escaping punctuation, I wrap the text in single quotes which tells PowerShell not to treat it as a special character. The spaces are part of the single quoted values.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7570\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4.png\" alt=\"timespan-4\" width=\"1191\" height=\"216\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4.png 1191w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4-300x54.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4-1024x186.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4-768x139.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-4-850x154.png 850w\" sizes=\"auto, (max-width: 1191px) 100vw, 1191px\" \/>Another option is to use the .NET format operator -f.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">\"{0:dd}d:{0:hh}h:{0:mm}m:{0:ss}s\" -f $up<\/code><\/pre>\n\n\n\n<p>The left side of the operator contains the replacement tokens. The right side contains the objects that are processed. The 0 tells PowerShell to use the first obect on the right side.&nbsp; Notice I'm using the same case-senstive tokens.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"95\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-5.png\" alt=\"timespan-5\" class=\"wp-image-7571\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-5.png 820w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-5-300x35.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-5-768x89.png 768w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/figure>\n\n\n\n<p>The -f operator is handy way of building string messages that you might want to write to a file or display as a message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1232\" height=\"117\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample.png\" alt=\"f-sample\" class=\"wp-image-7572\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample.png 1232w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample-300x28.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample-1024x97.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample-768x73.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/f-sample-850x81.png 850w\" sizes=\"auto, (max-width: 1232px) 100vw, 1232px\" \/><\/figure>\n\n\n\n<p>But getting back to my initial need, with this formatting information I could run commands like these to get better formatted output.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:ps mark:0 decode:true\"><code lang=\"powershell\" class=\"language-powershell\">$os | Select-Object CSName,LastBootUpTime,\n@{Name=\"Uptime\";Expression={((Get-Date) - $_.lastbootuptime).tostring(\"dd\\.hh\\:mm\")}}\n\n$os | Select-Object CSName,LastBootUpTime,\n@{Name=\"Uptime\";Expression={\"{0:dd}d:{0:hh}h:{0:mm}m:{0:ss}s\" -f ((Get-Date) - $_.lastbootuptime)}}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1145\" height=\"417\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png\" alt=\"timespan-6\" class=\"wp-image-7573\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6.png 1145w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6-300x109.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6-1024x373.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6-768x280.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/timespan-6-850x310.png 850w\" sizes=\"auto, (max-width: 1145px) 100vw, 1145px\" \/><\/figure>\n\n\n\n<p>In the first example I decided that even the number of seconds was irrelevant so I dropped them.<\/p>\n\n\n\n<p>One last comment is that even though my Uptime property started as a timespan, after this formatting I have turned the property into a string. My original code where I subtracted the LastBootUpTime property is still a timespan object. PowerShell just displays it as a string. This might affect your expression if you are piping to <strong>Sort-Object<\/strong> or <strong>Where-Object<\/strong>. Remember that Get-Member is your friend.<\/p>\n\n\n\n<p>And I hope I can remember I wrote this the next time I'm trying to recall how I formatted a timespan to strip off the milliseconds. As always, questions and comments are welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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,&nbsp; why not write about. Assuming I can remember! Anyway, here&#8217;s today&#8217;s &#8220;PSRemembery&#8221;. I often&nbsp; use code like this, and I expect many of&#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":"New on the blog: Formatting #PowerShell TimeSpans","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,540,126],"class_list":["post-7565","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-powershell","tag-scripting","tag-timespan"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Formatting PowerShell TimeSpans &#8226; The Lonely Administrator<\/title>\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\/7565\/formatting-powershell-timespans\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Formatting PowerShell TimeSpans &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"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,&nbsp; why not write about. Assuming I can remember! Anyway, here&#039;s today&#039;s &quot;PSRemembery&quot;. I often&nbsp; use code like this, and I expect many of...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-24T14:31:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-30T21:17:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.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\\\/7565\\\/formatting-powershell-timespans\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Formatting PowerShell TimeSpans\",\"datePublished\":\"2020-06-24T14:31:40+00:00\",\"dateModified\":\"2020-09-30T21:17:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/\"},\"wordCount\":552,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/uptime.png\",\"keywords\":[\"PowerShell\",\"Scripting\",\"TimeSpan\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/\",\"name\":\"Formatting PowerShell TimeSpans &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/uptime.png\",\"datePublished\":\"2020-06-24T14:31:40+00:00\",\"dateModified\":\"2020-09-30T21:17:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/uptime.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/uptime.png\",\"width\":925,\"height\":179},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7565\\\/formatting-powershell-timespans\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Formatting PowerShell TimeSpans\"}]},{\"@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":"Formatting PowerShell TimeSpans &#8226; The Lonely Administrator","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\/7565\/formatting-powershell-timespans\/","og_locale":"en_US","og_type":"article","og_title":"Formatting PowerShell TimeSpans &#8226; The Lonely Administrator","og_description":"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,&nbsp; why not write about. Assuming I can remember! Anyway, here's today's \"PSRemembery\". I often&nbsp; use code like this, and I expect many of...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/","og_site_name":"The Lonely Administrator","article_published_time":"2020-06-24T14:31:40+00:00","article_modified_time":"2020-09-30T21:17:12+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.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\/7565\/formatting-powershell-timespans\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Formatting PowerShell TimeSpans","datePublished":"2020-06-24T14:31:40+00:00","dateModified":"2020-09-30T21:17:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/"},"wordCount":552,"commentCount":7,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png","keywords":["PowerShell","Scripting","TimeSpan"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/","name":"Formatting PowerShell TimeSpans &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png","datePublished":"2020-06-24T14:31:40+00:00","dateModified":"2020-09-30T21:17:12+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/uptime.png","width":925,"height":179},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7565\/formatting-powershell-timespans\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Formatting PowerShell TimeSpans"}]},{"@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":170,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/170\/friendly-wmi-dates\/","url_meta":{"origin":7565,"position":0},"title":"Friendly WMI Dates","author":"Jeffery Hicks","date":"August 5, 2009","format":false,"excerpt":"Gee..you think you know something only to find out you don\u2019t. Or maybe this falls into the category of teaching an old dog new tricks. When I first started using PowerShell several years ago, I learned about how to convert a WMI date to a more user friendly format...","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7101,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7101\/converting-lexical-timespans-with-powershell\/","url_meta":{"origin":7565,"position":1},"title":"Converting Lexical Timespans with PowerShell","author":"Jeffery Hicks","date":"December 18, 2019","format":false,"excerpt":"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\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\/2019\/12\/image_thumb-26.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-26.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-26.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/image_thumb-26.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1653,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1653\/the-powershell-day-care-building-scriptblocks\/","url_meta":{"origin":7565,"position":2},"title":"The PowerShell Day Care: Building ScriptBlocks","author":"Jeffery Hicks","date":"September 22, 2011","format":false,"excerpt":"Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let's get out our blocks, our scriptblocks, and start building. I've written a number of posts\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7447,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7447\/get-installed-powershell-versions\/","url_meta":{"origin":7565,"position":3},"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":8059,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","url_meta":{"origin":7565,"position":4},"title":"Solving the PowerShell Conversion Challenge","author":"Jeffery Hicks","date":"January 19, 2021","format":false,"excerpt":"Today let's look at how I approached the first Iron Scripter PowerShell challenge of the year. The goal of the challenge was to convert or translate an object into a PowerShell class definition. If you are new to these challenges, the journey to how you achieve the goal is more\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\/2021\/01\/psobject.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":3551,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-3-0\/3551\/powershell-clean-up-tools\/","url_meta":{"origin":7565,"position":5},"title":"PowerShell Clean Up Tools","author":"Jeffery Hicks","date":"November 11, 2013","format":false,"excerpt":"A few years ago I think I posted some PowerShell clean up tools. These were functions designed to help clear out old files, especially for folders like TEMP. Recently I decided to upgrade them to at least PowerShell 3.0 to take advantage of v3 cmdlets and features. I use these\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"021913_2047_WordTest1.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/021913_2047_WordTest1.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7565","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=7565"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7565\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}