{"id":52543,"date":"2009-09-03T03:01:00","date_gmt":"2009-09-03T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/09\/03\/hey-scripting-guy-can-i-use-windows-powershell-to-pick-start-and-end-dates\/"},"modified":"2009-09-03T03:01:00","modified_gmt":"2009-09-03T03:01:00","slug":"hey-scripting-guy-can-i-use-windows-powershell-to-pick-start-and-end-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-windows-powershell-to-pick-start-and-end-dates\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?"},"content":{"rendered":"<p><SPAN class=\"sbmLink\">\n<TABLE cellSpacing=\"1\" cellPadding=\"1\">\n<TBODY>\n<TR>\n<TD class=\"sbmText\">Share this post: <\/TD>\n<TD><A title=\"Post it to Backflip!\" href=\"http:\/\/www.backflip.com\/add_page_pop.ihtml?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/backflip4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Buddymark!\" href=\"http:\/\/buddymarks.com\/s_add_bookmark.php?bookmark_url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;bookmark_title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/buddymar4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Del.icio.us!\" href=\"http:\/\/del.icio.us\/post?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/deliciou4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Digg!\" href=\"http:\/\/digg.com\/submit?phase=2&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/digg14.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Dotnetkicks!\" href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/CropperCapture154.jpg\"><\/A><\/TD>\n<TD><A title=\"Post it to Facebook!\" href=\"http:\/\/www.facebook.com\/sharer.php?u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;t=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/4982\/00\/00\/01\/76\/53\/19\/facebook.JPG\"><\/A><\/TD>\n<TD><A title=\"Post it to Furl!\" href=\"http:\/\/www.furl.net\/store?s=f&amp;to=0&amp;u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;ti=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/furl4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Netvouz!\" href=\"http:\/\/netvouz.com\/action\/submitBookmark?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/netvouz4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Reddit!\" href=\"http:\/\/reddit.com\/submit?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/reddit4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Shadow!\" href=\"http:\/\/www.shadows.com\/bookmark\/saveLink.rails?page=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><\/A><\/TD>\n<TD><A title=\"Post it to Spurl!\" href=\"http:\/\/www.spurl.net\/spurl.php?v=3&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/spurl8.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Technorati!\" href=\"http:\/\/technorati.com\/faves\/?add=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/technora4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Wists!\" href=\"http:\/\/www.wists.com\/?action=add&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;title=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wists9.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Yahoo!\" href=\"http:\/\/myweb.yahoo.com\/myresults\/bookmarklet?u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/03\/hey-scripting-guy-september-3-2009.aspx&amp;t=Hey, Scripting Guy! Can I Use Windows PowerShell to Pick Start and End Dates?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/yahoo9.png\"><\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/SPAN>\n<P class=\"MsoNormal\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/P>\n<P class=\"MsoNormal\">Hey, Scripting Guy! I need to be able to display a calendar so that I can pick starting dates and ending dates by using a graphical tool. This is much easier and faster than having to type two dates into the command line, and it is more accurate as well. Besides, when I am choosing dates, I always have to click the calendar in the lower right hand corner of my Windows 7 laptop to get a calendar anyway\u2014it would be great if I could directly choose the dates from that. What do you think? \n<P>&#8212; KB <BR><\/P>\n<P><SPAN><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><\/SPAN><\/P>\n<P class=\"MsoNormal\">Hello KB, <\/P>\n<P class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I think that as soon as I am finished writing my Windows PowerShell Best Practices book for Microsoft Press, I am going to start studying for the <A title=\"Exam 70-680\" href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/exam.aspx?ID=70-680&amp;locale=en-us\">Exam 70-680<\/A> that was released for Windows 7. I think it will be fun. There are no study materials available for it, so it will be just the computer and me. I will prepare for the exam by looking over the <A href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/exam.aspx?ID=70-680&amp;locale=en-us#tab2\"><FONT face=\"Segoe\">exam objectives<\/FONT><\/A> and making sure I know how to do everything. I could probably pass it without doing too much studying because I have been using Windows 7 for more than a year, but there are some things that I want to brush up on before I go take the exam.<SPAN>&nbsp; <\/SPAN>\n<P class=\"MsoNormal\">Oh, KB, you were wondering what I think about accessing a calendar? I think that it would be great if I could write a script that would detect the dates you select on your calendar at the lower corner of your laptop screen. However, I do not know of a way to do that. What I can do is create a portable calendar that you can use to select start and end dates that will feed back to your script. The Get-DateRangeFunction.ps1 script, seen here, does that very thing. \n<P class=\"CodeBlockScreenedHead\"><STRONG>Get-DateRangeFunction.ps1 <\/P><\/STRONG>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">Function Get-DateRange <BR>{<SPAN>&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>[System.Reflection.Assembly]::LoadWithPartialName(&#8220;System.windows.forms&#8221;) | <BR><SPAN>&nbsp;<\/SPAN>out-null <BR><SPAN>&nbsp;<\/SPAN> <BR><SPAN>&nbsp;<\/SPAN>$WinForm = New-Object Windows.Forms.Form<SPAN>&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$WinForm.text = &#8220;Calendar Control&#8221;<SPAN>&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$WinForm.Size = New-Object Drawing.Size(411,190) <BR><BR><SPAN>&nbsp;<\/SPAN>$Calendar = New-Object System.Windows.Forms.MonthCalendar<SPAN>&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$Calendar.MaxSelectionCount = 356<SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$Calendar.SetCalendarDimensions([int]2,[int]1) <BR><SPAN>&nbsp;<\/SPAN>$WinForm.Controls.Add($Calendar)<SPAN>&nbsp;&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN> <BR><SPAN>&nbsp;<\/SPAN>$WinForm.Add_Shown($WinForm.Activate())<SPAN>&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$WinForm.showdialog() | Out-Null<SPAN>&nbsp; <\/SPAN><BR><SPAN>&nbsp;<\/SPAN>$Calendar.SelectionRange <BR>} #end function Get-DateRange <BR><BR># *** Entry point to script *** <BR>$dates = Get-DateRange <BR>&#8220;Start Date: &#8221; + ($dates.Start).tostring(&#8220;dd-MM-yyyy&#8221;) <BR>&#8220;End Date: &#8221; + ($dates.End ).tostring(&#8220;dd-MM-yyyy&#8221;) \n<P><\/FONT><\/FONT><\/SPAN>The first thing we do in the Get-DateRangeFunction.ps1 script is create the <B>Get-DateRange<\/B> function. To do this, we use the <B>Function<\/B> keyword, and specify the Verb-Noun name pattern that is used by the Windows PowerShell cmdlets. In Windows PowerShell 2.0, if you use a name such as <B>GetMyDate<\/B> for your function, it will generate a warning message if you place this function in a module. Modules are a Windows PowerShell 2.0 feature that allow you to store functions in an easy-to-reuse fashion. When stored in modules, Windows PowerShell functions begin to act like cmdlets. In fact, the original name for advanced functions was <I>script cmdlet. <\/I>To get you ready for that experience, you should get into the habit of using the Verb-Noun naming convention for all of your functions. If you need inspiration or ideas you can use the <B>Get-Command<\/B> cmdlet and look at the functions, their names, and what the different verbs are doing. It is easier to read if you pipe the results to the <B>Sort-Object<\/B> and sort by verb. You might also consider selecting only the cmdlet name. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; Get-Command | Sort-Object -Property verb | Select-Object \u2013Property name \n<P><\/FONT><\/SPAN>Because you are typing this at the Windows PowerShell console, you might want to shorten this a little bit by using the built-in aliases. The revised command is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; gcm | sort verb | select name \n<P><\/FONT><\/SPAN>In Windows PowerShell 2.0, there is cmdlet named <B>Get-Verb<\/B> that will display a listing of approved verbs and how they are to be used. <\/P>\n<P>The command to create the <B>Get-DateRange<\/B> function and open the script block is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">Function Get-DateRange <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">{<SPAN>&nbsp;&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>In the <B>Get-DateRange<\/B> function, we use the <B>LoadWithPartialName<\/B> static method from the <B>System.Reflection.Assembly<\/B> .NET Framework class. This is done to make the <B>System.Windows.Forms<\/B> .NET Framework assembly available to the script. There are many assemblies that are not loaded by default by Windows PowerShell. This is done to increase the speed in which Windows PowerShell starts up and to reduce the memory footprint the application consumes. If you find yourself writing graphical Windows PowerShell scripts on a routine basis, you may want to load the <B>System.Windows.Forms<\/B> into your profile so that it will always be available. For more information about using the <B>LoadWithPartialName<\/B> static method, look at <A title=\"yesterday's Hey, Scriting Guy! article\" href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/02\/hey-scripting-guy-september-2-2009.aspx\">yesterday\u2019s Hey, Scripting Guy! post<\/A>. <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>[System.Reflection.Assembly]::LoadWithPartialName(&#8220;System.windows.forms&#8221;) | <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>Out-Null \n<P><\/FONT><\/SPAN>Once the <B>System.Windows.Forms<\/B> .NET Framework assembly has been loaded, you can create the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.form.aspx\">Windows.Forms.Form<\/A> .NET Framework class. The calendar control you will create in today&#8217;s script needs a form to host the control; therefore, you need to create the form first. To create the <B>Windows.Forms.Form<\/B> class, you use the <B>New-Object<\/B> cmdlet and supply the full name of the .NET Framework class. The newly created object is stored in the <B>$winForm <\/B>variable. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm = New-Object Windows.Forms.Form<SPAN>&nbsp;&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>Once you have created the form, you will want to assign a value to the <B>text<\/B> property. Because this script is demonstrating a calendar control, I decided to use that for the title. The <B>text<\/B> property is displayed in the top bar of the form. The property assignment is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm.text = &#8220;Calendar Control&#8221;<SPAN>&nbsp;&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>You now need to specify the size of the form. To specify the size of the form, you will need to supply the <B>size<\/B> property with an instance of a <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.drawing.size.aspx\">System.Drawing.Size structure<\/A>. When you use the <B>New-Object<\/B> cmdlet to create the <B>Drawing.Size<\/B> structure, you supply the height and the width of the rectangle that will be created. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm.Size = new-object Drawing.Size(411,190) \n<P><\/FONT><\/SPAN>You use the <B>New-Object<\/B> cmdlet to create an instance of the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.monthcalendar.aspx\"><FONT face=\"Segoe\">System.Windows.Forms.MonthCalendar<\/FONT><\/A> .NET Framework class. This class is used to create a calendar control that you will use on the form. You will need to save the returned object in the <B>$calendar<\/B> variable. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Calendar = new-object System.Windows.Forms.MonthCalendar<SPAN>&nbsp;&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>By default, the maximum number of days you can select is set to 7. To modify this behavior assign a new value for the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.monthcalendar.maxselectioncount.aspx\"><FONT face=\"Segoe\">MaxSelectionCount<\/FONT><\/A> property. In the Get-DateRangeFunction.ps1 script, I modify it to allow you to select 356 days if you are so inclined: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Calendar.MaxSelectionCount = 356<SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>The calendar dimensions control how the calendars will be displayed on your form. The <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.monthcalendar.setcalendardimensions.aspx\"><FONT face=\"Segoe\">SetCalendarDimensions<\/FONT><\/A> method controls the number of columns and rows of calendars that will be displayed. In the Get-DateRangeFunction.ps1 script, I create a display of calendars that is two calendars wide, but only has a single row. This method call is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Calendar.SetCalendarDimensions([int]2,[int]1) \n<P><\/FONT><\/SPAN>After the calendar dimensions have been set, it is time to add the <B>Calendar<\/B> to the Controls Collection of the form.<SPAN>&nbsp; <\/SPAN>To do this, you use the <B>Controls<\/B> property from the <B>Form<\/B> class, and call the <B>add<\/B> method from the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.control.controlcollection.aspx\"><FONT face=\"Segoe\">Windows.Forms.Form.Control.ControlClass<\/FONT><\/A>. After you have added the <B>Calendar<\/B> to the Controls Collection, you call the <B>Activate<\/B> method from the <B>Form<\/B> class. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm.Controls.Add($Calendar)<SPAN>&nbsp;&nbsp; <\/SPAN><\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm.Add_Shown($WinForm.Activate())<SPAN>&nbsp; <\/SPAN>\n<P><\/FONT><\/SPAN>You then call the <B>ShowDialog<\/B> method from the <B>Form<\/B> class to display the calendar. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$WinForm.showdialog() | Out-Null<SPAN>&nbsp; <\/SPAN><\/P><\/FONT><\/SPAN>\n<P class=\"MsoNormal\"><BR>When you pass your mouse over the calendar, you will select a range of dates. The range of dates is stored in the <B>SelectionRange<\/B> property of the <B>Calendar<\/B> control. It gets passed back to the calling code when you click the <B>Close<\/B> button in the upper right corner of the form. This is seen here: \n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Calendar.SelectionRange <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">} #end function Get-DateRange \n<P><\/FONT><\/SPAN>When the <B>Get-DateRange<\/B> function is called, you are presented with a display that shows two calendars side by side. Drag your mouse to select a range of dates. This is seen in here: <\/P>\n<P class=\"Fig-Graphic\"><IMG title=\"Image of two calendars presented side by side\" alt=\"Image of two calendars presented side by side\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0903\/hsg-09-03-09-01.jpg\" width=\"384\" height=\"185\"> <BR>\n<P>After you have clicked the <B>Close<\/B> button in the upper right corner of the dialog box, an instance of the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.selectionrange.aspx\"><FONT face=\"Segoe\">System.Windows.Forms.SelectionRange<\/FONT><\/A> class is returned to the calling code. In this example, I store the <B>SelectionRange<\/B> class in the <B>$dates<\/B> variable. I then take the <B>Start<\/B> date, and convert it to a string by using the <B>ToString<\/B> method. The <B>ToString<\/B> method will accept formatting to display the day, month, and year in a particular manner. The same thing is done with the end range. Both the <B>Start<\/B> property and the <B>End<\/B> property hold <B>System.DateTime<\/B> objects. The <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.datetime.tostring.aspx\"><FONT face=\"Segoe\">ToString<\/FONT><\/A> method from the <B>System.DateTime<\/B> class is able to receive different parameters that you can use to modify the way the date is displayed. This is seen here: <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">$dates = Get-DateRange <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">&#8220;Start Date: &#8221; + ($dates.Start).tostring(&#8220;dd-MM-yyyy&#8221;) <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">&#8220;End Date: &#8221; + ($dates.End ).tostring(&#8220;dd-MM-yyyy&#8221;) \n<P><\/FONT><\/SPAN>By using a <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/97x6twsz.aspx\"><SPAN><FONT face=\"Segoe\">DateTime format string<\/FONT><\/SPAN><\/A> or &#8220;dd-MM-yyyy&#8221; the day\/month\/year display seen here is produced: <\/P>\n<P class=\"Fig-Graphic\"><IMG title=\"Image of day\/month\/year display\" alt=\"Image of day\/month\/year display\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0903\/hsg-09-03-09-02.jpg\" width=\"600\" height=\"157\"> \n<P>KB, that is all there is to creating a calendar control and displaying it on a Windows form. Thank you for your question. This also wraps up the Graphical Windows PowerShell Week series of articles. Join us tomorrow as we dig into our e-mail bag and retrieve those questions that only need a short answer\u2014it is time once again for Quick-Hits Friday. <\/P>\n<P class=\"MsoNormal\">To stay abreast of all the exciting things happening on the TechNet Script Center, follow us on <A href=\"http:\/\/www.twitter.com\/scriptingguys\/\">Twitter<\/A> or <A href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\">Facebook<\/A>. If you have any questions, send e-mail to us at <A href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/A> or post it to the <A href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/A>. See you tomorrow. Until then, peace. \n<P>&nbsp;<\/P>\n<P><B><SPAN>Ed Wilson and Craig Liebendorfer, Scripting Guys <BR><\/SPAN><\/B><\/P>\n<P><B><SPAN><\/SPAN><\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Share this post: Hey, Scripting Guy! I need to be able to display a calendar so that I can pick starting dates and ending dates by using a graphical tool. This is much easier and faster than having to type two dates into the command line, and it is more accurate as well. Besides, when [&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":[71,3,4,45],"class_list":["post-52543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Share this post: Hey, Scripting Guy! I need to be able to display a calendar so that I can pick starting dates and ending dates by using a graphical tool. This is much easier and faster than having to type two dates into the command line, and it is more accurate as well. Besides, when [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52543","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=52543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52543\/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=52543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}