Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)?

share|improve this question
    
How does it work within the ISE? –  uSlackr Mar 30 '12 at 18:07

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
Certainly more work than I was wanting to do but a solid answer, thanks. –  jcollum Apr 5 '12 at 16:50

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.