Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying to the clipboard in PowerShell without a new line

Is there a way to remove a new line from out-clipboard or clip in PowerShell?

I'm using this code to copy current path to clipboard:

function cl() {
    (Get-Location).ToString() | clip
}

And every time I use this, a new line is added to the copied text. It's frustrating, because then I can't paste it in the CLI, like I would with text that is copied from elsewhere. Because a new line makes a command on the CLI automatically executed.

Example: I'm in C:\Users and type cl, and then I use Alt + SPACE + E + P to pass the text, the command is executed, and I can't type any more. But when text is passed without a new line nothing is executed, and I can continue to type.

like image 933
IGRACH Avatar asked Jan 01 '15 17:01

IGRACH


2 Answers

Use the Set-Clipboard function:

(get-location).ToString()|Set-Clipboard
like image 109
BdN3504 Avatar answered Oct 12 '22 05:10

BdN3504


Add-Type -Assembly PresentationCore
$clipText = (get-location).ToString() | Out-String -Stream
[Windows.Clipboard]::SetText($clipText)
like image 21
Adrian Rodriguez Avatar answered Oct 12 '22 05:10

Adrian Rodriguez