Remove blank lines in powershell output… Generally

时间秒杀一切 提交于 2021-02-04 22:00:51

问题


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

回答1:


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
}

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] v6+ - 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 *Core* (v6+).
    # 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 = $true)]
    [psobject] $InputObject
  )
  
  $null = $PSBoundParameters.Remove('InputObject')

  $Input | Out-String -Stream | Where-Object { $_.Trim().Length -gt 0 } | 
    Microsoft.PowerShell.Utility\Out-File @PSBoundParameters

}


来源:https://stackoverflow.com/questions/62833168/remove-blank-lines-in-powershell-output-generally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!