Home > Net >  How would I paste a long list of positive and negative numbers into powershell as one line?
How would I paste a long list of positive and negative numbers into powershell as one line?

Time:12-23

I have a long list of numbers that I need added and subtracted inside powershell but when I paste the numbers in to the shell they all end up showing on new lines. It looks like:

 15
-14
 2

I want it to look like 15-14 2 without having to go in and format each number. How would I do this?

I've tried running this as one variable and powershell reads each line as a separate number to echo and print rather than process.

CodePudding user response:

Assuming you trust what is on the clipboard, you can use Invoke-Expression (iex) as follows - though note that use of this cmdlet is generally to be avoided:

(Get-Clipboard) -join ' ' | Invoke-Expression
  • Get-Clipboard streams what is on the clipboard line by line.

  • -join ' ' joins the resulting lines to form a single-line string, with the input lines separated with ' ' (a single space).

  • Invoke-Expression evaluates the resulting single-line string as PowerShell code.

  • Related