title: Compare date against date in PowerShell
body: <p>I have a function that is for a maintenance window. I am trying to figure out the way I have done it, is actually the best way.</p>
  
  <p>I am passing in a date in the format "2015-05-07 01:00+13:00" and want to compare it against the date it is ran. i.e. today's date for this purpose.
  The date passed in will get passed in with a variable, and for testing I just manually enter it. Hence the HelpMessage.</p>
  
  <p>What I have so far I think works, I am sure there is a more efficient way of running what I want.</p>
  
  <p>The code is below:</p>
  
  <pre><code>Function Get-MaintenanceWindow {
  PARAM (
  [cmdletbinding()]
      [Parameter(Position=1,
          Mandatory=$True,
          HelpMessage="Enter maintenance window to compare against i.e 2015-05-07 01:00+13:00")]
      $Window
  )
      # Pass in two var and compare against, if $window1 &lt; $Window2 still in Window
      Write-Host ""
      Write-Host " * * * Running Function Maintenance Window * * * "
      #Compares two dates
      $TodaysDate = (Get-Date)
      $xTodaysDate = (Get-Date -Format yyyyMMddhhmm $TodaysDate)
      $xWindow = (Get-Date -Format yyyyMMddhhmm $Window)
  
      Write-Host "Today's date is : $xTodaysDate, compared to date is : $xWindow"
  
      Write-Host "Comparing against today's date $xTodaysDate against passed date $Window..."
      If ($xTodaysDate -lt $xWindow) {
          Write-host $xTodaysDate " is less than `$xWindow $xWindow, still in Maintenance Window!"
          $global:Check = $True
          }
      Else {
          Write-Host "Not in Maintenance Window, exiting..."
          #$Y = (Get-Date) +  " is less than `$Window $Window, still in Maintenance Window!"
          $global:Check = $False
          Break #Exits Function
      }
  }
  </code></pre>