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
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($_)
}
}
}