title: Powershell Invoke-Command resulting in RegEx error
body: <p>I'm using Invoke-Command to execute a powershell expression on a remote server. I'm getting a regex error when the command executes. It appears as though powershell uses RegEx internally to parse arguments, but for some reason, it's failing in this scenario.</p>
  
  <p>What's especially confusing is that the variable that appears to cause the parsing error isn't used at all in the Invoke-Command call. My guess is that some kind of variable capture is happening so that variables can be referenced in the remotely executing script (via the "using" scope), and this is failing for my $ArtifactDirectory parameter. I know it's the $ArtifactDirectory param causing the issue only because the value for the "regex expression" in the exception is the value of the $ArtifactDirectory parameter.</p>
  
  <p>Another important note is that I can run the script (with the exact same param values) manually on the exact same machine as TFS is executing it without error. This tells me it's some kind of session option causing the issue that TFS build/deployment agent is setting.</p>
  
  <p>Here is the relevant portion of the powershell script:</p>
  
  <pre><code>param(
      [string]    $ArtifactDirectory,
      [string]    $ComputerName
      )
  
  $someFancyCommand = "this does not seem to matter"
  
  Invoke-Command -ComputerName $ComputerName -ScriptBlock { 
      Invoke-Expression "$using:someFancyCommand"
  }
  </code></pre>
  
  <p>And here is how I'm executing it (in this case, via TFS Release Management):</p>
  
  <pre><code>MyScript.ps1 -ArtifactDirectory "C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App" -ComputerName "SomeComputer"
  </code></pre>
  
  <p>Here is the full stack trace:</p>
  
  <pre><code>2016-04-25T13:53:28.6024146Z [Exception:System.Management.Automation.RuntimeException: The regular 
  2016-04-25T13:53:28.6024146Z expression pattern {{C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App}} is not valid. ---&gt; 
  2016-04-25T13:53:28.6024146Z System.ArgumentException: parsing "{{C:\MSAgent\_work\66f1e4ebb\Cc - (WIP)\CC - My Cool App}}" - 
  2016-04-25T13:53:28.6024146Z Unrecognized escape sequence \M.
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.RegexParser.ScanCharEscape()
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.RegexParser.ScanBasicBackslash()
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.RegexParser.ScanRegex()
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, TimeSpan matchTimeout, Boolean 
  2016-04-25T13:53:28.6024146Z useCache)
  2016-04-25T13:53:28.6024146Z    at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options)
  2016-04-25T13:53:28.6024146Z    at System.Management.Automation.ParserOps.NewRegex(String patternString, RegexOptions options)
  2016-04-25T13:53:28.6024146Z    at System.Management.Automation.ParserOps.ReplaceOperator(ExecutionContext context, IScriptExtent errorPosition, 
  2016-04-25T13:53:28.6226512Z Object lval, Object rval, Boolean ignoreCase)
  2016-04-25T13:53:28.6226512Z    --- End of inner exception stack trace ---
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception 
  2016-04-25T13:53:28.6226512Z exception)
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
  2016-04-25T13:53:28.6226512Z    at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean 
  2016-04-25T13:53:28.6336645Z createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior 
  2016-04-25T13:53:28.6336645Z errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo 
  2016-04-25T13:53:28.6336645Z invocationInfo, Object[] args)
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.ScriptBlock.&lt;&gt;c__DisplayClassa.&lt;InvokeWithPipe&gt;b__8()
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior 
  2016-04-25T13:53:28.6336645Z errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo 
  2016-04-25T13:53:28.6336645Z invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[]
  2016-04-25T13:53:28.6336645Z  args)
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, 
  2016-04-25T13:53:28.6336645Z ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
  2016-04-25T13:53:28.6336645Z    at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessRecord()
  2016-04-25T13:53:28.6336645Z    at System.Management.Automation.CommandProcessor.ProcessRecord()]
  </code></pre>