15

The following command wraps the output to the width of the window from which the script was called. That is, the output file is "word"-wrapped. How can I prevent this wrapping in the output file w/o modifying the script?

PS C:\Users\User1> & '\\fileServer\c$\PowerShell Scripts\herScript.ps1' > output.txt
lance
  • 16,092
  • 19
  • 77
  • 136

3 Answers3

16

Try this (I can't test it)

& '\\fileServer\c$\PowerShell Scripts\herScript.ps1' | out-string -width 4096 | out-file c:\output.txt
CB.
  • 58,865
  • 9
  • 159
  • 159
  • 6
    `Out-File` has a `-Width` parameter, too. Its documentation states that everything beyond the width is truncated (not wrapped). So I guess this truncates first at 4096 characters (what an oddly non-round number) and then at the width of the console window. While it won't wrap it does truncate longer lines which may not be intended. – Joey Jan 23 '12 at 16:36
  • 11
    4096 is not odd and not non-round. It's 2^12 – Bruno Bronosky Nov 30 '16 at 21:19
  • The `-Width` parameter doesn't seem to prevent line-wrapping. Injecting `Out-String -Width xxx` in the pipe just before `Out-File` does do the trick for me though. – JimNim Nov 16 '18 at 17:46
6

Instead of using >, which is out-file, you can use set-content

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    Caveat lector: the behaviour of `Set-Content` differs. Significantly (but not documented) it locks the file so it can't be read. Hence `Set-Content` is a bad choice for logging. See http://stackoverflow.com/questions/10655788/powershell-add-content-and-out-file-what-is-the-difference – Colonel Panic Jul 10 '12 at 09:41
2

Use the Write-Host cmdlet as the last statement of your pipeline. Normal unadorned powershell output appears to look at the dimensions of the parent console window, and trims/wraps output lines to width-1. The Write-Host cmdlet bypasses this step and writes directly to stdout without any further munging.

Here's an example, which reads a JSON file, and writes javascript output which adds the JSON to a big string (preserving comments):

powershell -Command "$input | ForEach-Object { \"manifestBlob += \"\"\" + ($_ -replace \"\"\"\", \"\\\"\"\") + \"\n\"\";\" } | Write-Host" < manifest.json > buildmanifest.js

Here's a sample input file:

//  File: manifest.json
//
//  Description:
//      manifest for chrome plug-in

{
    "manifest_version": 2,

    "version": "0.0.0",

    // the ID is: sdfjkghsdfjkghjksdfghjkfhjkdfjff
    "key": "sdfjkhsdfjkghjksdfghkjsdhgsdjkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsdfjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl",

    "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["content.js"], "run_at": "document_start" } ],

    // this is the standard LOCAL install location - but if this extension is published to the app-store, this value gets overridden (that is okay and even good)
    "update_url": "file:///C:/Program%20Files/MyProduct/Update.xml",
}

And the output when using Write-Host:

manifestBlob += "\n";
manifestBlob += "//  File: manifest.json\n";
manifestBlob += "//\n";
manifestBlob += "//  Description:\n";
manifestBlob += "//      manifest for chrome plug-in\n";
manifestBlob += "\n";
manifestBlob += "{\n";
manifestBlob += "    \"manifest_version\": 2,\n";
manifestBlob += "\n";
manifestBlob += "    \"version\": \"0.0.0\",\n";
manifestBlob += "\n";
manifestBlob += "    // the ID is: sdfjkghsdfjkghjksdfghjkfhjkdfjff\n";
manifestBlob += "    \"key\": \"sdfjkhsdfjkghjksdfghkjsdhgsdjkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsdfjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl\",\n";
manifestBlob += "\n";
manifestBlob += "    \"content_scripts\": [ { \"matches\": [\"http://*/*\", \"https://*/*\"], \"js\": [\"content.js\"], \"run_at\": \"document_start\" } ],\n";
manifestBlob += "\n";
manifestBlob += "    // this is the standard LOCAL install location - but if this extension is published to the app-store, this value gets overridden (that is okay and even good)\n";
manifestBlob += "    \"update_url\": \"file:///C:/Program%20Files/MyProduct/Update.xml\",\n";
manifestBlob += "}\n";

And finally, an example of the terrible things which happen if you leave out the Write-Host cmdlet (assuming a console width of 60):

manifestBlob += "\n";
manifestBlob += "//  File: manifest.json\n";
manifestBlob += "//\n";
manifestBlob += "//  Description:\n";
manifestBlob += "//      manifest for chrome plug-in\n";
manifestBlob += "\n";
manifestBlob += "{\n";
manifestBlob += "    \"manifest_version\": 2,\n";
manifestBlob += "\n";
manifestBlob += "    \"version\": \"0.0.0\",\n";
manifestBlob += "\n";
manifestBlob += "    // the ID is: sdfjkghsdfjkghjksdfghjkf
hjkdfjff\n";
manifestBlob += "    \"key\": \"sdfjkhsdfjkghjksdfghkjsdhgs
djkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsd
fjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl\",\n";
manifestBlob += "\n";
manifestBlob += "    \"content_scripts\": [ { \"matches\": 
[\"http://*/*\", \"https://*/*\"], \"js\": [\"content.js\"]
, \"run_at\": \"document_start\" } ],\n";
manifestBlob += "\n";
manifestBlob += "    // this is the standard LOCAL install 
location - but if this extension is published to the app-st
ore, this value gets overridden (that is okay and even good
)\n";
manifestBlob += "    \"update_url\": \"file:///C:/Program%2
0Files/MyProduct/Update.xml\",\n";
manifestBlob += "}\n";
William
  • 690
  • 5
  • 13