title: How to catch error from Remove-Item and emit a warning instead?
body: <p>In a post-deployment script used in a continuous integration pipeline (Azure DevOps), I'm removing old files.</p>
  
  <p>Basically, it's a PowerShell script that removes every release folder but the current one in the deployment directory.</p>
  
  <p>Sometimes, the Remove-Item fails for some reason (old file still opened by someone one the deplyoment machine, for instance)</p>
  
  <p>It's not a big deal. I don't want an error saying my whole deployment failed because of this. However, I want a warning, so I'm aware that it happened.</p>
  
  <p>For instance (MCVE): </p>
  
  <pre><code>Remove-Item INEXISTENT_FILE
  </code></pre>
  
  <p><strong>Problem</strong> : it causes an error.</p>
  
  <hr>
  
  <p>Attempt 1 :</p>
  
  <pre><code>Remove-Item INEXISTENT_FILE -ErrorAction SilentlyContinue
  </code></pre>
  
  <p><strong>Problem</strong> : It removes the Error completely, that's not what I want (I want a warning)</p>
  
  <hr>
  
  <p>Attempt 2 : I tried to use ErrorVariable as recommended here : <a href="https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/" rel="noreferrer">https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/</a></p>
  
  <pre><code>Remove-Item INEXISTENT_FILE -ErrorAction SilentlyContinue -ErrorVariable $removeItemError
  if ($removeItemError) {
      Write-Warning "Warning, something failed!"
  }
  </code></pre>
  
  <p><strong>Problem</strong> : it doesn't work, it doesn't show the <code>if</code> part. If I remove "SilentlyContinue" error action, it just emits an error, and in any case never goes into the <code>if</code> part.</p>
  
  <hr>
  
  <p>Attempt 3 : I tried to use also Try Catch block as proposed here : <a href="https://stackoverflow.com/questions/23000356/powershell-erroraction-silentlycontinue-does-not-work-with-get-aduser">PowerShell -ErrorAction SilentlyContinue Does not work with Get-ADUser</a></p>
  
  <pre><code>Try {
      Remove-Item INEXISTENT_FILE
  }
  Catch {
      Write-Warning "Warning, something failed!"
  }
  </code></pre>
  
  <p><strong>Problem</strong> : it never goes into the catch block either (!?)</p>
  
  <hr>
  
  <p>Anyone has another option to show a warning instead of an error if Remove-Item fails ?</p>