Using -Confirm in Powershell
<p>I am trying to understand what the relationship between the <code>$?</code> and <code>$lastexitcode</code> variables versus the <code>-Confirm</code> flag in Powershell cmdlets.</p>

<p>Say for example you run a command with <code>-confirm</code> it will prompt you accordingly for action:  </p>

<pre class="lang-none prettyprint-override"><code>PS C:\temp&gt; rm .\foo.txt -confirm
Confirm

Are you sure you want to perform this action?

Performing the operation "Remove Directory" on target "C:\temp\foo.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):n

PS C:\temp&gt; $?

True
</code></pre>

<p>I understand that technically the command ran successfully, but if the user chose no then the command did not run.  </p>

<p>My question is how to I obtain the user's answer to the <code>-Confirm</code> flag?</p>