title: Using Switch Case to Replace If Condition
body: <p>I would like to understand more about switch statement and how to implement it. I have the following raw code to set the paging file. I would like to change the if and elseif condition after "Write-Host Windows 2008 edition detected" to switch case.</p>
  
  <p>Is it possible to do that with Switch statement? I have tried different formula on that, but still couldn't find the perfect code.</p>
  
  <p>Here is the code. </p>
  
  <pre><code>$OSbuild = [environment]::OSVersion.Version.Build ## Checking the OS version
  $Drives = ".\Drives.txt"
  $Cdrive = "C: Hard Drive"
  $Ddrive = "D: Hard Drive"
  $Pdrive = "P: Hard Drive"
  $OthDrive1 = "[^DP]: Hard Drive"
  $OthDrive2 = "[^CDP]: Hard Drive"
  
  
  $Disk = Get-WmiObject win32_logicaldisk 
  
  Foreach ($Drive in $Disk) 
      {
      Switch ($Drive.DriveType) 
          {
          1{$Disks = $Drive.DeviceID + " Unknown"; Break} 
          2{$Disks = $Drive.DeviceID + " Floppy or Removable Drive"; Break} 
          3{$Disks = $Drive.DeviceID + " Hard Drive"; Break} 
          4{$Disks = $Drive.DeviceID + " Network Drive"; Break} 
          5{$Disks = $Drive.DeviceID + " CD"; Break} 
          6{$Disks = $Drive.DeviceID + " RAM Disk"; Break} 
          }
      Write $Disks &gt;&gt; $Drives 
      }
  
  if (Get-Content $Drives | ? {$_ -like $Pdrive})
      {
      Proces A
      }
  
  elseif ($OSbuild -eq 6001 -or 7601)
      {
      Write-Host "Windows 2008 edition detected."
      $OSversion = "2008"
  
      if (Get-Content $Drives | ? {$_ -like $Ddrive}) ## Multiple drives found (with D) in the server
          {
          Process B
          }
  
      elseif (Get-Content $Drives | ? {$_ -match $OthDrive2}) ## Multiple drives found (without D) in the server
          {
          Process C
          }
  
      elseif (Get-Content $Drives | ? {$_ -like $Cdrive -notmatch $OthDrive1}) ## Only C drive found in the server
          {
          Process D
          }
  
      else
          {
          Write "Couldn't find the paging file rule for this server!"
          }
      }
  </code></pre>