Commandline syntax to prevent wrapping in PowerShell output file?

20,057

Solution 1

Try this (I can't test it)

& '\\fileServer\c$\PowerShell Scripts\herScript.ps1' | out-string -width 4096 | out-file c:\output.txt

Solution 2

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

Solution 3

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";
Share:
20,057
lance
Author by

lance

Twitter: http://twitter.com/@lancehilliard

Updated on February 18, 2020

Comments

  • lance
    lance about 4 years

    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
    
  • Rich
    Rich over 12 years
    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.
  • Colonel Panic
    Colonel Panic almost 12 years
    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 stackoverflow.com/questions/10655788/…
  • Bruno Bronosky
    Bruno Bronosky over 7 years
    4096 is not odd and not non-round. It's 2^12
  • JimNim
    JimNim over 5 years
    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.
  • Der_Meister
    Der_Meister almost 3 years
    Nothing is redirected to the file when I use Write-Host. PowerShell 5.1.