84

Sometimes I want to copy a command from Powershell to paste in a document, or I want to copy the output? How can I select and copy text in Powershell?

At least I know a way how to paste a text (or a command) into Powershell: you just right-click on Powershell.

2
  • 2
    You may want to move non-SharePoint related answers to the more generic Stack Overflow site. I do appreciate you are probably using PowerShell in combination with SharePoint, but this questions doesn't belong here. Jun 22, 2011 at 8:57
  • I agree. It is not a pure Sharepoint question.
    – mirontoli
    Jun 22, 2011 at 11:33

9 Answers 9

62

Just select the text in the console window and press enter or the right mouse button. That selected text ends up in your clipboard.

Note that this will only work if QuickEdit mode is enabled for the console window. If it is not, then either enable it in the console window properties (System menu → Properties → Options) or enter Mark mode via System menu → Edit → Mark (Alt+Space, E, K on an English Windows).

2
  • Using the shift key to highlight the text (text turns white) didn't work for me. I had to use the mouse to highlight the text (text turns orange) Apr 16, 2019 at 16:42
  • How do I select all text in the window? Nov 23, 2019 at 0:59
31

Or send the output of your command directly to clipboard using clip.exe For example,

Get-ChildItem C:\Test -recurse | Clip
7
  • 1
    a pipe with clip i a good alternative, too! Thank you!
    – mirontoli
    Jun 22, 2011 at 11:30
  • 7
    This will not work with Unicode, though. In that case you can use Set-Clipboard from PSCX.
    – Joey
    Jun 23, 2011 at 8:00
  • 7
    There IS a way to have clip.exe handle Unicode correctly: set PS's $OutputEncoding variable to BOM-less UTF-16 first, as follows: $OutputEncoding = New-Object System.Text.UnicodeEncoding($false, $false)
    – mklement0
    Mar 22, 2013 at 18:57
  • This last trick is what I looked for coming here ! Thanks : )
    – sodawillow
    May 18, 2015 at 18:12
  • 2
    @Timo: Set-Clipboard is long-since included in PowerShell. Haven't installed PSCX in ages, but it was once handy, back in the PowerShell 2/3 days
    – Joey
    Nov 17, 2020 at 21:12
11

Go to the menubar, top left, Edit, Select All, Copy, paste in notepad

1
  • This worked for me. Thank you. Aug 24, 2020 at 16:01
2

Have a look at Send Text in Clipboard to Application like Notepad (C# or Powershell). You will find some more tips. However, answer by @Wictor is probably the easiest solution.

2

Set-Clipboard is standard cmdlet as of Powershell v5.0. In some cases you should convert objects to text with Out-String before piping result to the clipboard:

Get-ChildItem C:\Windows -recurse -depth 1 | Out-String -stream | Set-Clipboard
1

I've build my own out-clipboard funciton for this.

Function Out-Clipboard{
    param($Value,[switch]$PassThru) 
    begin {
            [void][reflection.assembly]::LoadWithPartialName("Windows.Forms")
        $tb = New-Object System.Windows.Forms.TextBox
        $tb.Multiline = $true
        $pipeObjects = @()
    }
    process {
      $pipeObjects+=$_
    }
    end {
        if([string]::IsNullOrEmpty($Value)){
            $text=$null
            $pipeObjects | out-string -stream | %{$text = $text + $(if($text -ne $null){"`r`n"}) + $_}
            $tb.text = $text
        } 
        else {
            $tb.text = $value
        }
        $tb.SelectAll()
        $tb.Copy()
        if($PassThru){
            $pipeObjects
        }
        $tb.Dispose()
    }
}

Sample command line:

Get-Process | Out-Clipboard

Hope it's what you're looking for.

3
  • Your code doesn't work on my box: New-Object : Cannot find type [System.Windows.Forms.TextBox]: make sure the assembly containing this type is loaded. At line:5 char:25 + $tb = New-Object <<<< System.Windows.Forms.TextBox + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
    – Winfred
    Jun 23, 2011 at 20:55
  • 1
    If you check out pscx.codeplex.com , one of the functions provided by the community extensions is out-clipboard
    – Winfred
    Jun 23, 2011 at 20:56
  • 1
    It is very impressive, @CosmosKey! Thanks for mentioning pscx.codeplex.com, @Winfred! Jun 26, 2011 at 13:03
0

If you want to copy the last command you typed to the clipboard, the following command is useful (especially for commands that span multiple lines):

(Get-History -Count 1).CommandLine | Set-Clipboard

If you want to repeat the last command, and copy its output to the clipboard, use:

Invoke-History | Set-Clipboard

Caveat: you should only do this for inexpensive and idempotent commands without any side-effects.

0

Old question, but this may help someone. For Windows 10, the Terminal application offers an auto-copy option in Settings > Interaction > Automatically copy selection to clipboard.

To paste inside Powershell, you can use Ctrl+V or Shift+Insert, no need to use the mouse.

0

Did Windows 11 really remove the menu bar from Powershell?

Anyway, to select all, use Ctrl+Shift+A, and to copy and paste are both Right Clicks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .