title: execute if statement in variable value assignement powershell
body: <p>Sometimes I need to check if a pathname ends with "\" adding it if necessary, the code is pretty simple, something like this</p>
  
  <pre><code>if ($destFolder[-1] -ne '\') {
      $destFolder += '\';
  }
  </code></pre>
  
  <p>Is there a way to evaluate an if statement inside <code>()</code> so that I can use it in variable assignement? I mean something like this</p>
  
  <pre><code>$finalName = $destFolder + (if ($destFolder[-1] -ne '\') { '\' } ) + $fileName
  </code></pre>
  
  <p>Given that <code>if</code> is not a cmdlet I get this error</p>
  
  <blockquote>
    <p>if : The term 'if' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
    path is correct and try again.</p>
  </blockquote>