Unexpected Behavior When Pasting Multiple Lines of PowerShell Code When you copy multiple lines of PowerShell code and paste them into a shell window, the...
Unexpected Behavior When Pasting Multiple Lines of PowerShell Code
When you copy multiple lines of PowerShell code and paste them into a shell window, the result often is not what you expect. PowerShell starts executing the first line and won’t execute the pasted code en bloc. Try and look at the default behavior by copying the code below, then pasting it into a PowerShell console window:
Each line of the pasted block is executed individually which you can see when you look at the prompt displayed before each output line.
Ensuring the Entire Code Block Executes as One
While this default behavior can be OK, if you must ensure that your entire code block is executed in one, embed it into curly brackets and execute this script block with the “.” operator. Try and copy this embedded code: