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
There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.
Try this (I can't test it)
& '\\fileServer\c$\PowerShell Scripts\herScript.ps1' | out-string -width 4096 | out-file c:\output.txt
Instead of using >
, which is out-file
, you can use set-content
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With