{"id":215,"date":"2014-12-11T00:01:00","date_gmt":"2014-12-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/12\/11\/use-a-powershell-function-to-determine-miles-to-go\/"},"modified":"2019-02-18T10:36:38","modified_gmt":"2019-02-18T17:36:38","slug":"use-a-powershell-function-to-determine-miles-to-go","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-a-powershell-function-to-determine-miles-to-go\/","title":{"rendered":"Use a PowerShell Function to Determine Miles to Go"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about a Windows PowerShell function to determine miles remaining to meet challenge.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. If you are friends with me on Facebook, you know that in October I accepted a challenge to run a hundred miles before January 31, 2015. I was doing really well until I was knocked out of the action by surgery. The other day the doctor gave me a clearance to begin exercising again. Now, I need to know how many miles I need to do, per day, to complete the 100-mile challenge.<\/p>\n<p>When I was on the treadmill this morning, I decided that this was the perfect challenge for Windows PowerShell&mdash;determining how many miles to run a day (well, not actually running the miles&#8230;Jeffery Hicks aside, Windows PowerShell looks pretty strange on a pair of running shoes).<\/p>\n<p><span style=\"font-size:12px\">The <\/span><b style=\"font-size:12px\">Get-Miles<\/b><span style=\"font-size:12px\"> Windows PowerShell function is really simple. I did not even add any Help to it because I will be using it for the next month or so, and then it will have outlived its usefulness.<\/span><\/p>\n<p>So how does it work?<\/p>\n<p>The first thing I do is use the <b>Function<\/b><i> <\/i>keyword to tell Windows PowerShell I want to create a function. Next, I assign a name. I like to use Verb-Noun naming conventions when creating function names because it promotes function discoverability.<\/p>\n<p>I create a single input parameter. This input parameter is my total mileage that I have run to date. Obviously, this will change on a daily basis (or at least I hope it will change on a daily basis). When I run, I always run complete miles. So it will be something like 1, 2, 3, or 4 miles. I do not do partial miles, so I cast my input type as an integer. Here is my script so far:<\/p>\n<p style=\"margin-left:30px\">Function Get-Miles {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Param ([int]$miles)<\/p>\n<p>Everything else in the function is automatic because I know the following information:<\/p>\n<ul>\n<li>The end date. I have to complete the challenge by January 31, 2015. This is a static value that will not change.<\/li>\n<li>The distance. I have to complete 100 miles. This is also a static value that will not change.<\/li>\n<li>The current date. Every time I run the function, I want to know how many miles I will need to do per day, to reach my goal within the allotted time. I am always basing my answer on the current date.<\/li>\n<\/ul>\n<p>I create two variables to hold my static values. I could create constants instead of variables for this&mdash;but hey, there is no real reason for doing this. I am not going to change the values, so they will be constant enough. Here are the value assignments:<\/p>\n<p style=\"margin-left:30px\">$end = &quot;1\/31\/2015&quot;<\/p>\n<p style=\"margin-left:30px\">$distance = 100<\/p>\n<p>Now I create a <b>TimeSpan<\/b> object that will represent the amount of time between now and January 31, 2015. I am only interested in the number of days&mdash;and <b>Days<\/b><i> <\/i>is one of the properties of a <b>TimeSpan<\/b> object. I store the number of days in a variable that I call <b>$days<\/b>. Here is the command:<\/p>\n<p style=\"margin-left:30px\">$days = (New-TimeSpan -Start (get-date) -End ([datetime]$end)).Days<\/p>\n<p>I now need to do a little bit of math work. I want to calculate the number of miles per day that I need to run to meet my goal of 100 miles by the end of January. To do this, I use my static distance value of 100 (stored in the <b>$distance<\/b> variable), and I subtract the number of miles I have run to date.<\/p>\n<p>Because I will know this value when I call the function, I use the variable that holds my parameter here. Then I divide by the number of days remaining (which I obtained from my <b>TimeSpan<\/b> object). Here is the command:<\/p>\n<p style=\"margin-left:30px\">$milesPerDay = ($distance &#8211; $miles)\/ $days<\/p>\n<p>The last thing to do is format my output. Because I am not interested in a large number of digits of output (in fact, the treadmill at the gym only displays tenths of miles), the answer I need is mileage that is precise to a tenth of a mile. If I go over the requisite amount each day (remember I always run to a complete mile), I will be assured of meeting my goal. Here is the line of code that formats the output:<\/p>\n<p style=\"margin-left:30px\">&quot;Run {0:N1} miles per day&quot; -f $milesPerDay}<\/p>\n<p>The complete function is shown here:<\/p>\n<p style=\"margin-left:30px\">Function Get-Miles {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Param ([int]$miles)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $end = &quot;1\/31\/2015&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $distance = 100<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $days = (New-TimeSpan -Start (get-date) -End ([datetime]$end)).Days<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $milesPerDay = ($distance &#8211; $miles)\/ $days<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;Run {0:N1} miles per day&quot; -f $milesPerDay}<\/p>\n<p>I could add it to my Windows PowerShell profile&mdash;but I will be only using this function every few days for the next few weeks, so it is not worth the trouble. I will, however, copy it to my scratch folder so it will be easy-to-use.<\/p>\n<p>I could call it from the command line in the Windows PowerShell console. But for me, it is easier to simply open it up in the Windows PowerShell ISE. I run the function by pressing the big green triangle, and then I call the function from the immediate pane. The following image provides an example of using the function:<\/p>\n<p><span style=\"font-size:12px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-11-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-11-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>&nbsp;<\/span><\/p>\n<p>That is all there is to using Windows PowerShell to determine miles left to run. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a Windows PowerShell function to determine miles remaining to meet challenge. Microsoft Scripting Guy, Ed Wilson, is here. If you are friends with me on Facebook, you know that in October I accepted a challenge to run a hundred miles before January 31, 2015. I was doing [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,3,4,45],"class_list":["post-215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a Windows PowerShell function to determine miles remaining to meet challenge. Microsoft Scripting Guy, Ed Wilson, is here. If you are friends with me on Facebook, you know that in October I accepted a challenge to run a hundred miles before January 31, 2015. I was doing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}