can I get powershell to properly copy multiline text like Bash in Ubuntu does?

1

I use an Ubuntu VM at home for Rails development and I've gotten a bit spoiled by Bash. When I copy multiline text from Bash, it seems like it's pretty good about knowing that text is single line or multiline when I copy it.

In Powershell (hosted in Console2) on Win7, I don't get this behavior. Note how HRESULT is cut in two in this error message (scroll to the right):

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(1558,9): warning MSB3284: Cannot get the file
path for type library "fdca4b6c-605a-4b76-adce-68010c4a2581" version 4.0. Library not registered. (Exception from HRESU
LT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)) [C:\Dev\Foo.csproj]

Is there a way to get Powershell to figure out that it shouldn't cut words in half? Is this behavior only available in Bash and I should just use Bash in Windows (maybe CygWin)?

powershell
powershell-2.0
console2
asked on Super User Mar 30, 2012 by jcollum

2 Answers

2

I know this doesn't address the issue of directly copying text that has already been printed to the console, but one option is to pipe the output to Out-String -Width <columns> to prevent long lines from being wrapped. Then pipe the output to clip.exe to copy it to the clipboard.

If, for example, I type an invalid command and I get an error message that I want to copy:

PS> Blah
The term 'Blah' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:1
+ Blah
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (Blah:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I can get the last error message with $Error[0],

$Error[0]| Out-String -Width 10000| clip.exe

and this is what I get when I paste into notepad (note the first line is not wrapped).

The term 'Blah' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Blah
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (Blah:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
answered on Super User Mar 30, 2012 by Rynant
0

Powershell's copy behavior is block based, rather than line based, and it unfortunately is not able to link wrapped lines back together. However, while I do like bash (and fish) for scripting, Powershell's deep integration with Windows Services and Servers makes it a more appropriate shell to use despite it's less capable interface. Perhaps someone else is aware of a Powershell replacement that retains the integration with Windows objects but also has a better interface; I am not.

answered on Super User Mar 30, 2012 by Myrddin Emrys

User contributions licensed under CC BY-SA 3.0