title: Using PowerShell 'If' statement to compare function (Windows version)
body: <p>I'm currently creating an <a href="http://en.wikipedia.org/wiki/Active_Directory" rel="nofollow">AD</a> script that can get the AD groups of one machine and transfer them to a new machine (in case of system failure).</p>
  
  <p>I've managed to get the script to go out and find the versions of Windows that the two machines are running via their hostname, however I'm having a problem creating an 'if' statement to compare the two versions of Windows.</p>
  
  <p>The idea is that if the are the same version (and thus the same package version) the groups will be copied automatically, but I can't for  the life of me figure out how to do it.</p>
  
  <p>Please consider this code:</p>
  
  <pre><code>function W_version_current
  {
      $current = Get-WmiObject Win32_OperatingSystem -computer $current_hostname.text | select buildnumber
      if ($current -match '7601')
      {
          "Windows 7"
      }
      elseif($current -match '2600')
      {
          "Windows XP"
      }
      elseif($current -eq $null)
      {
          "The box is empty"
      }
      else
      {
          "Function not supported"
      }
  }
  
  function W_version_target
  {
      $target = Get-WmiObject Win32_OperatingSystem -computer $target_hostname.text | select buildnumber
      if ($var -match '7601')
      {
          "Windows 7"
      }
      elseif($target -match '2600')
      {
          "Windows XP"
      }
      elseif($target -eq $null)
      {
          "The box is empty"
      }
      else
      {
          "Function not supported"
      }
  }
  
  function compare_current_target
  {
      if(W_version_current -eq W_version_target)
      {
          "Matching version of Windows detected"
      }
      else
      {
          "Versions of Windows do not match"
      }
  }
  </code></pre>
  
  <p>Now is it true that all variables cannot be accessed outside of functions?</p>
  
  <p>If so, what else can I do?</p>