totalolage
totalolage

Reputation: 27

Powershell output to clipboard without trailing newline?


I need to output the content of a powershell variable to the clipboard, preserving all the newline characters except for the last -trailing- one.
At the moment I am just piping the output of a variable readout to clip.exe, but that gives a trailing newline.

$Text = "line1`nline2"
$Text | clip.exe

gives the following:

"line1,
line2
"

I would like it to output

"line1,
line2"

How might I achieve this?

Upvotes: 2

Views: 1397

Answers (1)

Jason Snell
Jason Snell

Reputation: 1465

Using the pipeline can result in a new line being added by powershell. You can use Set-Clipboard and it should avoid the newline issue.

You can also use the .NET option as well:

[System.Windows.Forms.Clipboard]::SetText("line1`r`nline2")

Upvotes: 5

Related Questions