title: How to run Powershell script in VS Code using Powershell Integrated Console
body: <p>I have this little script, which works fine in PowerShell (outside of VS Code):</p>
  
  <pre><code>$a = @{Portfolio = "CALoan"; Folder = "S:\Data\{yymmdd}"; Filename = "LN{yymmdd}.txt"}
  $l = @($a)
  $l | ForEach-Object -Process {
      $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
      if (Test-Path $p) {
          [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}
      } else {
          [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}
      }
  } | Out-GridView
  </code></pre>
  
  <p>However, when viewing the script in the VSCode editor, if I just right-click and choose Run Code (using the Code Runner extension),  I get tons of errors like this:</p>
  
  <pre>
  PS C:\Users\me> $l | ForEach-Object -Process {
  Missing closing '}' in statement block or type definition.
  At line:0 char:0
  PS C:\Users\me>     $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
  PS C:\Users\me>     if (Test-Path $p) {
  Missing closing '}' in statement block or type definition.
  At line:0 char:0
  PS C:\Users\me>         [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}
  
  Portfolio Path CreateTime
  --------- ---- ----------
            \    {2/28/2019 12:41:46 PM, 2/20/2019 12:32:15 PM, 1/24/2019 3:54:50 PM, 3/15/2019 1:46:40 PM...}
  
  
  PS C:\Users\me>     } else {
  At line:1 char:1
  + } else {
  + ~
  Unexpected token '}' in expression or statement.
  
  At line:1 char:8
  + } else {
  +        ~
  Missing closing '}' in statement block or type definition.
  PS C:\Users\me>         [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}
  
  Portfolio Path CreateTime
  --------- ---- ----------
            \    not found
  
  
  PS C:\Users\me>     }
  At line:1 char:1
  + }
  + ~
  Unexpected token '}' in expression or statement.
  PS C:\Users\me> } | Out-GridView
  At line:1 char:1
  + } | Out-GridView
  + ~
  Unexpected token '}' in expression or statement.
  
  At line:1 char:3
  + } | Out-GridView
  +   ~
  An empty pipe element is not allowed.
  PS C:\Users\me>
  </pre>
  
  <p>It's as if the integrated terminal is executing one line at a time rather than sending the whole script to PowerShell.  What is the right way to do this?</p>
  
  <p>BTW If I start a new PowerShell terminal within VS Code, it works as expected (also using Code Runner).  So, what's up with the PowerShell Integrated Console and Code Runner?</p>