Copying to the clipboard in PowerShell without a new line Copying to the clipboard in PowerShell without a new line powershell powershell

Copying to the clipboard in PowerShell without a new line


Use the Set-Clipboard function:

(get-location).ToString()|Set-Clipboard


Add-Type -Assembly PresentationCore$clipText = (get-location).ToString() | Out-String -Stream[Windows.Clipboard]::SetText($clipText)


As pointed out by @PetSerAl in the comments, the newline is added by PowerShell when the string object is sent through the pipeline. The stringified output of Get-Location does not have that trailing newline:

PS C:\> $v = (Get-Location).ToString()PS C:\> "-$v-"-C:\-

You could try something like this:

Add-Type -AssemblyName System.Windows.Forms$tb = New-Object Windows.Forms.TextBox$tb.MultiLine = $true$tb.Text = (Get-Location).ToString()$tb.SelectAll()$tb.Copy()