title: Converting Parts of an Array in powershell
body: <p>I'm supposed to tag this as homework, but I'm not sure how. Not asking for an answer, just to be pointed in the right direction of what I'm doing wrong. Basically, this is supposed to make a complete sentence out of ANY user input, with the exception of -AllCaps, which is functioning properly. My problem is the other part.</p>
  
  <pre><code>param ($title, [switch]$AllCaps)
  $ex="to|a|the|at|in|of|with|and|but|or"
  function Proper($ts)
  {
  $nt=foreach($word in $ts)
  {
      $word=$word.ToLower()
  
      if($word -match $ex)
      {
          if($word -eq $ts[0])
          {
              $letters=$word -csplit("")
              $letters[1]=$letters[1].ToUpper()
              $word=$letters -join("")
          }
          else
          {
              $word=$word
          }
      }
      else
      {
          $letters=$word -csplit("")
          $letters[1]=$letters[1].ToUpper()
          $word=$letters -join("")
      }
      "$word"
  }
  $nt=$nt -join(" ")
  Write-host $nt
  }
  
  if($AllCaps)
  {
  $title=$title.ToUpper()
      "$title"
  }
  else
  {
      $ts=$title -split(" ")
      $newtitle=Proper $ts
  }     
  </code></pre>
  
  <p>So, when I execute the script passing in "the waffle of kings", expected output is "The Waffle of Kings" - instead, the code seems to be completely ignoring the "else" in the first "if else" statement, and the output I'm getting is "The waffle of kings". Since "waffle" doesn't match anything in $ex, it should be moving to the else part, capitalizing the first letter.</p>
  
  <p>Less of note is that it doesn't write to the console without including "$word" within the foreach loop, though the way I have it written, the Write-Host should be doing that for me. If I take out the write-host nothing changes, but if I take out the "$word" in the loop, I get no output at all.</p>