Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 2 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

You must log in to answer this question.

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