Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1108 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I output text without a newline in PowerShell?

#1
I want my PowerShell script to print something like this:

Enabling feature XYZ......Done

The script looks something like this:

Write-Output "Enabling feature XYZ......."
Enable-SPFeature...
Write-Output "Done"

But `Write-Output` always prints a new-line at the end so my output isn't on one line. Is there a way to do this?
Reply

#2
<PRE>
Write-Host -NoNewline "Enabling feature XYZ......."
</PRE>
Reply

#3
While it may not work in your case (since you're providing informative output to the user), create a string that you can use to append output. When it's time to output it, just output the string.

Ignoring of course that this example is silly in your case but useful in concept:

$output = "Enabling feature XYZ......."
Enable-SPFeature...
$output += "Done"
Write-Output $output

Displays:

Enabling feature XYZ.......Done
Reply

#4
A simplification to FrinkTheBrave's response:

[System.IO.File]::WriteAllText("c:\temp\myFile.txt", $myContent)
Reply

#5
The following will place the cursor back at beginning of the previous row. It's up to you to place it in the right horizontal position (using $pos.X to move it sideways):

$pos = $host.ui.RawUI.get_cursorPosition()
$pos.Y -= 1
$host.UI.RawUI.set_cursorPosition($Pos)

Your current output is 27 spaces over, so <b>$pos.X = 27</b> might work.
Reply

#6
You can absolutely do this. Write-Output has a flag called "**NoEnumerate**" that is essentially the same thing.
Reply

#7
You simply cannot get PowerShell to omit those pesky newlines... There is no script or cmdlet that does. Of course, [Write-Host][1] is absolute nonsense, because you can't redirect/pipe from it!

Nevertheless, you can write your own EXE file to do it which is what I explained how to do in Stack Overflow question *https://stackoverflow.com/questions/2038181/how-to-output-something-in-powershell/11901810#11901810*.

[1]:

[To see links please register here]


Reply

#8
To write to a file you can use a byte array. The following example creates an empty ZIP file, which you can add files to:

[Byte[]] $zipHeader = 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
[System.IO.File]::WriteAllBytes("C:\My.zip", $zipHeader)

Or use:

[Byte[]] $text = [System.Text.Encoding]::UTF8.getBytes("Enabling feature XYZ.......")
[System.IO.File]::WriteAllBytes("C:\My.zip", $text)


Reply

#9
The problem that I hit was that Write-Output actually linebreaks the output when using using PowerShell v2, at least to stdout. I was trying to write an XML text to stdout without success, because it would be hard wrapped at character 80.

The workaround was to use

[Console]::Out.Write($myVeryLongXMLTextBlobLine)

This was not an issue in PowerShell v3. Write-Output seems to be working properly there.

Depending on how the PowerShell script is invoked, you may need to use

[Console]::BufferWidth =< length of string, e.g. 10000)
before you write to stdout.
Reply

#10
The answer by shufler is correct. Stated another way: Instead of passing the values to Write-Output using the ARRAY FORM,

Write-Output "Parameters are:" $Year $Month $Day

or the equivalent by multiple calls to Write-Output,

Write-Output "Parameters are:"
Write-Output $Year
Write-Output $Month
Write-Output $Day
Write-Output "Done."

concatenate your components into a STRING VARIABLE first:

$msg="Parameters are: $Year $Month $Day"
Write-Output $msg

This will prevent the intermediate CRLFs caused by calling Write-Output multiple times (or ARRAY FORM), but of course will not suppress the final CRLF of the Write-Output commandlet. For that, you will have to write your own commandlet, use one of the other convoluted workarounds listed here, or wait until Microsoft decides to support the `-NoNewline` option for Write-Output.

Your desire to provide a textual progress meter to the console (i.e. "....") as opposed to writing to a log file, should also be satisfied by using Write-Host. You can accomplish both by collecting the msg text into a variable for writing to the log AND using Write-Host to provide progress to the console. This functionality can be combined into your own commandlet for greatest code reuse.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through