{"id":13571,"date":"2011-06-20T00:01:00","date_gmt":"2011-06-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/20\/top-ten-favorite-powershell-tricks-part-3\/"},"modified":"2011-06-20T00:01:00","modified_gmt":"2011-06-20T00:01:00","slug":"top-ten-favorite-powershell-tricks-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/top-ten-favorite-powershell-tricks-part-3\/","title":{"rendered":"Top Ten Favorite PowerShell Tricks: Part 3"},"content":{"rendered":"<h2><b>Working with Ranges, Dates, and Other Cool PowerShell Tricks<\/b><\/h2>\n<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, concludes his three-part series about his top ten favorite Windows PowerShell tricks.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I got a little carried away with my top ten list of Windows PowerShell tricks over the weekend. I had originally intended it to be only a Weekend Scripter series, but as I began writing about the tricks, I wanted to tell you WHY the tricks are my favorite tricks, and then I wanted to illustrate different things that show how to use the tricks. Before I knew it, I had run out of space. Rather than wait until next weekend to complete the series, I decided to run the blogs consecutively. So this is part three of my series about my top ten Windows PowerShell Tricks.<\/p>\n<p>In the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/18\/top-ten-favorite-powershell-tricks-part-1.aspx\" target=\"_blank\">first blog<\/a>, I talked about using the transcript feature, finding hidden objects, using <b>Select<\/b> to expand properties, and grouping items so they are dot accessible. In the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/19\/top-ten-favorite-powershell-tricks-part-2.aspx\" target=\"_blank\">second blog<\/a>, I continued my discussion about the my top ten Windows PowerShell tricks by talking about using a square bracket to index into an array, using tab expansion, and using the <i>List<\/i> parameter with the <b>Get-WmiObject<\/b> cmdlet to find WMI classes.<\/p>\n<h3>Trick #8: Use the range operator<\/h3>\n<p>One of my favorite things to do is to create an array of 10 elements with the value of 1 through 10 in just five key strokes. Here is the code.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">1..10<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>When I run this code, it creates an array of numbers, numbered consecutively 1 through 10. If I store these numbers into a variable, I can index into the array and retrieve the value of the elements. Remember that an array is zero based; and therefore, <b>$a[5]<\/b> returns the sixth item in the array. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1440.hsg-06-20-11-01_323231F0.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4276.hsg-06-20-11-01_thumb_440EC5BD.png\" width=\"604\" height=\"272\" \/><\/a><\/p>\n<p>If I need to create an array of negative numbers, I use a minus in front of the numbers. This is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; -1..-5<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-2<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-3<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-4<\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-5<\/span><\/span><\/span><\/p><\/blockquote>\n<p>I can reverse the order in which the numbers are created as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; -5..-1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-5<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-4<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-3<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-2<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">-1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; 4..1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">4<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">3<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">2<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>There are times when I need to perform a command multiple times&mdash;for example, when refreshing Group Policy while waiting for replication to occur. In the following command, I call <b>GPUpdate<\/b> five times. I display a string to the Windows PowerShell console to let me know what pass has occurred and the time that pass ran. I also pause the script for ten seconds to wait between iterations of the command. The <b>%<\/b> character is an alias for the <b>ForEach-Object<\/b> cmdlet. The <b>;<\/b> character is a command separator (it is an end-of-logical-line terminator). Therefore, the command inside the curly brackets is actually three separate commands.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">1..5 | % { gpupdate ; &#8220;update $_ $(get-date)&#8221; ; sleep 10 }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>When the command runs, the following output is produced on my system.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7823.hsg-06-20-11-02_6ADCDBFD.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.hsg-06-20-11-02_thumb_11AAF23E.png\" width=\"604\" height=\"416\" \/><\/a><\/p>\n<p>One day, I needed to create an array of letters. I thought I would try my range operator trick, and so I typed the command shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">A..Z<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Unfortunately, the range operator does not work for letters, only for numbers. I then remembered the ASCII table, and so I modified the command a bit, and cast the numbers to characters. The revised command is shown here (remember the <b>%<\/b> symbol is an alias for the <b>ForEach-Object<\/b> cmdlet. The <b>[char]<\/b> casts the number to a System.Char .NET Framework type).<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">65..90 | % { [char]$_ }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>These commands and their associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3034.hsg-06-20-11-03_185DFBC1.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5661.hsg-06-20-11-03_thumb_63516C90.png\" width=\"604\" height=\"416\" \/><\/a><\/p>\n<h3>Trick # 9 Get-AllHelpExamples<\/h3>\n<p>I am certain that you are familiar with using the <b>Get-Help<\/b> cmdlet. I use the <b>Get-Help<\/b> cmdlet on a regular basis. I can find Help about any cmdlet or function by supplying the name of the cmdlet or function as an argument as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-Help Get-Service<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>One thing that is pretty cool is that I can also use an alias for the argument as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-Help gsv<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>If I need to see a complete list of Help for a particular cmdlet, I use the <i>Full<\/i> switched parameter. Because this will generate a lot of text, when I am in the Windows PowerShell console, I can pipe the results to the pager as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-Help gsv &ndash;full | more<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The pager displays one page of text at a time, and halts with a <b>More<\/b> prompt. Pressing the space bar will cause it to advance to the next page of text. If I know that I will use the full display, I use the <b>Help<\/b> function. That&rsquo;s right, <b>Help<\/b> is a function, not an alias. What the help function does is incorporate the pager. Therefore, the following two commands are equivalent.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-Help gsv &ndash;full | more<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Help gsv -full<\/span><\/span><\/span><span>&nbsp;<\/span><\/p>\n<\/blockquote>\n<p>One of my favorite tricks involves pairing the <b>Get-Command<\/b> cmdlet and the <b>Get-Help<\/b> cmdlet together. I get a list of all the cmdlets on a system, pipe them to the <b>Get-Help<\/b> cmdlet, and view the examples. It is rather cool. But, instead of calling <b>Get-Help<\/b> with the <i>Examples<\/i> parameter, I reference the <b>MamlCommandHelpInfo<\/b> object and retrieve the <i>Examples<\/i> property. It is cool. Here is the command I like to use.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-Command -CommandType cmdlet | % { (get-help $_.name).examples }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<h3>Trick #10 Converting WMI Dates<\/h3>\n<p>One of the things people like to complain about with WMI is the way that dates are displayed. I can agree with them&hellip;to an extent. An example of a WMI date is shown here. Note that the first four characters are the year, followed by two for the month and two for the day. If you hold your head sideways and squint, you might be able to read the date without too much problem.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; (gwmi win32_operatingsystem).installdate<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">20110604220039.000000-240<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>So the format is a bit strange&hellip;that is not too bad. What is a real pain is that it is not a <b>System.DateTime<\/b> object, rather it is a plain old string. This makes calculations based on dates a bit cumbersome.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; (gwmi win32_operatingsystem).installdate.gettype()<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">IsPublic IsSerial Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>BaseType<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">True<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>True<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>String<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Object<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>In the old days, the Scripting Guys wrote a function that was included in the ScriptOMatic. This function would parse the string and convert it into some sort of intelligible format. In Windows XP, WMI introduced a method to convert the string into a date, but it was rather difficult to use, and most people did not know about it anyway&mdash;so they continued to use the Scripting Guys function.<\/p>\n<p>When Windows PowerShell 1.0 came out, I was overjoyed to find the <b>ConvertToDateTime<\/b> method built onto the WMI object. For example, if I want to convert the install date into a regular date, I store the WMI object in a variable, and then call the <b>ConvertToDateTime<\/b><i> <\/i>function while passing it the <b>InstallDate <\/b>property. This is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; $os = gwmi win32_operatingsystem<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; $os.ConvertToDateTime($os.installDate)<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Saturday, June 04, 2011 10:00:39 PM<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>More than just translating the date, it actually converts the object into a real <b>System.DateTime<\/b> object. This is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; ($os.ConvertToDateTime($os.installDate)).gettype()<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">IsPublic IsSerial Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>BaseType<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">True<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>True<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DateTime<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.ValueType<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Because I have an actual <b>DateTime <\/b>object, I can use it to perform date calculations. For example, if I want to see how long the operating system has been installed, I pass <b>InstallDate<\/b> to the <b>New-TimeSpan<\/b> cmdlet as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; new-Timespan $os.ConvertToDateTime($os.installDate)<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Days<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Hours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 20<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Minutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 51<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Seconds<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 31<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Milliseconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 174<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Ticks<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2478911740086<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">TotalDays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2.86911081028472<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">TotalHours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 68.8586594468333<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">TotalMinutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4131.51956681<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">TotalSeconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 247891.1740086<\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">TotalMilliseconds : 247891174.0086<\/span><\/span><\/span><\/p><\/blockquote>\n<p>This concludes my top ten Windows PowerShell Tricks. This list is accurate as of today. I learn new things about Windows PowerShell every single day, and so this list is likely to change over time. If you happen to see me somewhere and you want an easy way to start a conversation, just ask me what cool things I have learned about Windows PowerShell recently. I guarantee that will get me talking&mdash;getting me to shut up, well, that is a different story.<\/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\">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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with Ranges, Dates, and Other Cool PowerShell Tricks Summary: Microsoft Scripting Guy, Ed Wilson, concludes his three-part series about his top ten favorite Windows PowerShell tricks. Microsoft Scripting Guy, Ed Wilson, is here. I got a little carried away with my top ten list of Windows PowerShell tricks over the weekend. I had originally [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45,77],"class_list":["post-13571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>Working with Ranges, Dates, and Other Cool PowerShell Tricks Summary: Microsoft Scripting Guy, Ed Wilson, concludes his three-part series about his top ten favorite Windows PowerShell tricks. Microsoft Scripting Guy, Ed Wilson, is here. I got a little carried away with my top ten list of Windows PowerShell tricks over the weekend. I had originally [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13571","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13571"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13571\/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=13571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}