-4

I am a newcomer to PS scripting. I have found that, generally speaking, PS adds a lot of newlines to output of its commands. I am giving below a couple of examples of what I found somewhat generalized about the (default?) output in PS commands. I am used to Unix-like output, where the generalized output has few to none of these lines (of course, nothing prevents a programmer from adding them).

Is there a configurable way to reduce the amount of those newlines, and perhaps the verbosity?

I have compiled below a list of SO questions (and extra) dealing with specific cases, but none aims at a general setting. I could apply some of the answers on a command-by-command basis, but I am asking if there is some form of overall setting/configuring PS.

EDIT: Later on I found this similar question (with a slight variation) in SU.


Examples

This is what I see with a couple of commands

> dir
                                                                                                    ******  EXTRA LINE
                                                                                                    ******  EXTRA LINE
    Directorio: C:\Users\USER1\OneDrive - YPF\Documents\soft-hard-ware\windows\powershell           ******  VERBOSITY LINE
                                                                                                    ******  EXTRA LINE
                                                                                                    ******  EXTRA LINE
Mode                LastWriteTime         Length Name                                               ******  VERBOSITY LINE
----                -------------         ------ ----                                               ******  VERBOSITY LINE
-a----         9/7/2020     17:11           1021 exec_powershell_info.txt
-a----         9/4/2016     08:25            281 first_bytes.ps1
-a----        10/7/2020     07:53           1536 get_current_script_name.ps1
-a----         9/7/2020     11:58             29 hello_world.ps1
-a----         9/4/2016     09:02            344 last_bytes.ps1
-a----        14/4/2016     13:08            975 split.ps1
-a----        10/7/2020     07:54           3108 update-help.txt
                                                                                                    ******  EXTRA LINE
                                                                                                    ******  EXTRA LINE
> Get-Culture
                                                                                                    ******  EXTRA LINE
LCID             Name             DisplayName                                                       ******  VERBOSITY LINE
----             ----             -----------                                                       ******  VERBOSITY LINE
11274            es-AR            Español (Argentina)                                                                  
                                                                                                    ******  EXTRA LINE
                                                                                                    ******  EXTRA LINE

Scrolling to the right, one can see my comments. I have tagged "EXTRA LINE" the lines I mean to remove. I have tagged "VERBOSITY LINE" the lines I mean to remove, but which I sort of "tolerate" more.

As a point in comparison, this is what I see in Linux, which I find "less verbose".

$ ll
total 12K
drwxrwxr-x  2 santiago santiago 4,0K ago 10  2017 ./
drwxrwxr-x 17 santiago santiago 4,0K may 19 10:28 ../
-rwxrwxr-x  1 santiago santiago  557 ago 10  2017 date2str.rb*

List of other sources that do not address the issue

  1. Remove blank lines in powershell output
  2. Remove blank lines Powershell output
  3. Removing Blank Lines From Powershell Output
  4. How to remove extra lines from Get-WMIObject output powershell
  5. Remove blank lines from output?
  6. Remove Blank Space From Output in PowerShell
  7. Remove Empty Lines From Powershell Output
  8. Removing extra blank space from PowerShell output
  9. Remove blank lines from the output
  10. https://social.technet.microsoft.com/Forums/en-US/229727f6-fdb6-4d95-b585-bd9af3ee6670/removing-blank-spaces-and-blank-lines-from-output?forum=ITCG
22
  • 4
    Are you essentially asking all of the readers to visit ten links read through all of the information on them, then suggest something which has not been mentioned in all of those? Asking for general solutions to encompass every possible output is just not a realistic question. If you have a specific issue with a particular output, we can try to provide you with a solution. Other than essentially reprogramming PowerShell, or programming a helper application or script which takes everything output, parses it, modifies it and then outputs it as you want, your question is probably unanswerable. Commented Jul 10, 2020 at 11:52
  • 1
    I've never noticed an excess of blank lines in PowerShell. Can you give an example of where you see this? As for the verbose output, that depends on the command (and switches) you run, but PowerShell provides a lot of scope for customising or suppressing it. Again, can you give an example where you have verbose output and what you'd actually like to see instead? Commented Jul 10, 2020 at 11:52
  • 3
    please recall that what you see on screen IS NOT THE OBJECT. it's a text representation of the object and may have items added AND/OR removed to make the display human readable. so ... what EXACT problem are you dealing with? is it the on-screen display or the actual data? Commented Jul 10, 2020 at 12:55
  • For display purposes only, send everything to Out-String and Trim() the excess. (Command | Out-String).Trim(). However, I don't recommend changing the output type of your objects. I'd just live with it as it is. Commented Jul 10, 2020 at 13:01
  • 1
    @Compo - "Are you essentially asking all of the readers...?" No, why would you think so? I just posted the list to make it explicit that I have read them and, unless I missed something, there is no solution to my OP there. Commented Jul 10, 2020 at 14:54

1 Answer 1

6

You can define a custom Out-Default function that removes all empty/blank lines from the output (for display only):

function Out-Default { 
  $Input | Out-String -Stream | Where { $_.Trim().Length -gt 0 } | Write-Host
}

Note: This simple implementation buffers all pipeline input first, which is suboptimal; for a proper but more complex, streaming implementation using a proxy function, see this answer.

You can place it in your $PROFILE file to be available in all future sessions (except those where PowerShell is invoked with -NoProfile).

All commands implicitly use this function for to-host (console) output, because Out-Default is a special command that PowerShell calls behind the scenes for handling to-host output (that is, for output that is neither captured nor piped nor redirected).

Therefore, once the function has been defined, simply calling Get-ChildItem (which is aliased to dir), for instance, should produce output with all empty/blanks removed.

Note that capturing / piping / redirecting output is not affected, as is desirable.

Caveat: As stated, Out-Default only affects to-host output, not also the behavior of other formatting system-based Out-* cmdlets, which you'd have to redefine separately, if desired.

Notably, a redefined Out-Default does not affect Out-File and its effective aliases > / >> - see the bottom section for a custom Out-File implementation that performs the same stripping of blank lines.


As an aside: PowerShell comes with a default Out-Default command, implemented as a cmdlet. While you can call Out-Default directly (whether the default implementation or a custom one), there is no reason to, and doing so should be avoided - see this answer.


As Compo points out, you can use the same technique ad hoc, for a given command; e.g.:

Get-ChildItem | Out-String -Stream | Where { $_.Trim().Length -gt 0 }

Note, however, that - unlike the Out-Default approach - this technique is suitable for display only, because you're then outputting string representations rather than the original objects (and if you were to pipe to Write-Host, you would bypass the (success) output stream altogether).


Custom Out-File function that also strips empty/blank lines and is also called by > / >>:

Note:

  • This function is more complex than the custom Out-Default function, because it needs to support the same parameters as the original cmdlet.

  • It will be noticeably slower than the original Out-File / > / >>, and it doesn't support the common -Confirm and -WhatIf parameters (which are rarely used with Out-File, however).

  • As printed below, the function is designed for use in PowerShell (Core) 7 - see the comments in the source code for how to (easily) adapt it to Windows PowerShell.

# Note: Supports neither -Confirm nor -WhatIf
function Out-File {
  [CmdletBinding(DefaultParameterSetName = 'ByPath', HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2096621')]
  param(
    [Parameter(ParameterSetName = 'ByPath', Mandatory, Position = 0)]
    [Alias('Path')]
    [string] $FilePath,
  
    [Parameter(ParameterSetName = 'ByLiteralPath', Mandatory, ValueFromPipelineByPropertyName)]
    [Alias('PSPath', 'LP')]
    [string]
    $LiteralPath,
  
    [Parameter(Position = 1)]
    # Note: This validation set is for *PowerShell 7*.
    # For Windows PowerShell support, comment out the the next line and uncomment the one after.
    [ValidateSet('ascii', 'bigendianutf32', 'unicode', 'utf8', 'utf8NoBOM', 'bigendianunicode', 'oem', 'utf7', 'utf8BOM', 'utf32')]
    # [ValidateSet('default', 'ascii','bigendianutf32','unicode','utf8','bigendianunicode','oem','utf7','utf32')]
    [string] $Encoding,
  
    [switch] $Append,
  
    [switch] $Force,
  
    [Alias('NoOverwrite')]
    [switch] $NoClobber,
  
    [ValidateRange(2, 2147483647)]
    [int] $Width,
  
    [switch] $NoNewline,
  
    [Parameter(ValueFromPipeline)]
    [psobject] $InputObject
  )
  begin {
    try {
      $steppablePipeline = { 
        Out-String -Stream | Where-Object { $_.Trim().Length } | 
          Microsoft.PowerShell.Utility\Out-File @PSBoundParameters
      }.GetSteppablePipeline($myInvocation.CommandOrigin)
      $steppablePipeline.Begin($PSCmdlet)
    }
    catch {
      $PSCmdlet.ThrowTerminatingError($_)
    }
  }
  process {
    try {
      $steppablePipeline.Process($InputObject)
    }
    catch {
      $PSCmdlet.ThrowTerminatingError($_)
    }
  }
  end {
    try {
      $steppablePipeline.End()
    }
    catch {
      $PSCmdlet.ThrowTerminatingError($_)
    }
  }
}
Sign up to request clarification or add additional context in comments.

14 Comments

Interesting point... When I redirect output with, e.g., Get-Module -ListAvailable > modules.txt, extra newlines do not get trimmed.
jdhitsolutions.com/blog/powershell/3025/… might be of interest here. Still have to understand a lot about PS...
Interesting point #2... I have added your function at the top of my profile.ps1 and it works great. Now I added $Host a few lines below, launched a new console, and the output of $Host does not have the newlines trimmed.
@sancho.sReinstateMonicaCellio: That was my bad: as the answer states, Out-Default only covers to-host output; >, which is an effective alias of Out-File, is not affected (and neither are other Out-* cmdlets, such as Out-String); in short: you'd have to write a wrapper for Out-File as well; though it looks if you want > / >> to respect that too, you'll have to write a proxy (wrapper) function - see this answer.
I will try this and get back whenever I have anything worth commenting. This code goes well beyond my most elementary knowledge on PS.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.