title: Getting the arguments of the last invoked command in powershell?
body: <p>I want to be able to get the argument portion of the previous command. <code>$^</code> seems to return just the command and not the args. <code>Get-History -count 1</code> returns the last full command including the command and the args. I could just .Replace the first instance, but I am not sure if it is correct.</p>
  
  <p>Scenario is that sometimes I want to do something like this. Let's assume that $* are the args to the last command:</p>
  
  <pre><code>dir \\share\files\myfile.exe
  copy $* c:\windows\system32
  </code></pre>
  
  <p>Any ideas how to get the last args correctly?</p>
  
  <p>UPDATE: finished my method for doing this.</p>
  
  <pre><code>function Get-LastArgs
  {
      $lastHistory = (Get-History -count 1)
      $lastCommand = $lastHistory.CommandLine   
      $errors = [System.Management.Automation.PSParseError[]] @()
  
      [System.Management.Automation.PsParser]::Tokenize($lastCommand, [ref] $errors) | ? {$_.type -eq "commandargument"} | select -last 1 -expand content    
   }
  </code></pre>
  
  <p>Now I can just do:</p>
  
  <pre><code>dir \\share\files\myfile.exe
  copy (Get-LastArgs) c:\windows\system32
  </code></pre>
  
  <p>To reduce typing, I did</p>
  
  <pre><code>set-alias $* Get-LastArgs
  </code></pre>
  
  <p>so now I still have to do </p>
  
  <pre><code>copy ($*) c:\windows\system32
  </code></pre>
  
  <p>if anybody has any ideas for making this better please let me know.</p>