0

How to suppress all the uninvited output? I recently started coding on PS and this obsessive ubiquitous output everywhere drives me crazy. I have to spam | Out-Null, [void] all around the codebase keeping an eye on every call because otherwise, the function will return a corrupted value with a bunch of parasite neighbors. Like a developer mainly on traditional languages, I don't understand the pleasure of seeing in the terminal a whole bunch of bullshit like 0, 7582645, ============ etc. Ideally, I want that output will fires only when I explicitly ask it for - echo/print/Write-Host and nothing more. I've tried some workarounds but with no luck. A tiny synthetic example:

function out-default
{
    $input | out-null
}
$PSDefaultParameterValues = @{
    'New-Item:OutVariable' = 'Null'
    'Disabled' = $False
}

function get
{
    "1"
    [Text.Encoding]::ASCII.GetBytes( "test" )
    return "value"
}

get

<#
the output actual: 1 116 101 115 116 value
the output wanted: value
#>

Anybody save me, please :(

MegaBomber
  • 345
  • 2
  • 3
  • 11
  • Why do you need a line like `"1"` if you do not want to print it? Shall it rather be a comment? Then prefix it with `#`. – stackprotector Jun 07 '23 at 15:39
  • It's just a sample of what I face all the time. Many functions behave exactly this way and spamming the terminal – MegaBomber Jun 07 '23 at 15:39
  • Then please post a [MRE]. – stackprotector Jun 07 '23 at 15:41
  • Don't sure I understand what wrong with my sample, but added an external function call – MegaBomber Jun 07 '23 at 15:44
  • 1
    There is no way to let only output that is produced by the `return` statement through, because in PowerShell, `return "value"` is just syntactic sugar for `"value"; return`. Though you may block a code block from producing output like this: `$null = . { multiple statements here }`. – zett42 Jun 07 '23 at 15:45
  • Ok, can I at least suppress output by default for all calls in the script? – MegaBomber Jun 07 '23 at 15:47
  • You can suppress it at the top level function, e. g. `$null = get` or using a code block (see edited comment above). – zett42 Jun 07 '23 at 15:49
  • @MegaBomber Your "mre" lacks a real application example. There is no need for the line `"1"`, if you do not want to print it. There is also no need for the line `[Text.Encoding]::ASCII.GetBytes( "test" )` if you do not want to print its result. It will be something different, if you assign those line to varaibles and process them further. If you assign them to variables, they will also not be printed anymore. – stackprotector Jun 07 '23 at 15:52
  • I know, but it's looks like crutches – MegaBomber Jun 07 '23 at 15:53

1 Answers1

1

I actually answered this question here: https://stackoverflow.com/a/75542335/4891401

TL;DR (but also too long)

If you just type the variable 'freely', then it will output the value, just like if you had put Write-Host in front of it.

Example:

$a = "Hello"
$a

# output: Hello

If you don't want to output $a, then using the example above:

$a = "Hello"

# output: 

A few points about your question; we cannot see important code to distinguish what is happening in your script, e.g. What is the value of $input? Also, all of the commands you are running are not being assigned to anything, so obviously, it will output it's return value into the terminal. Below is an attempt to remove this from your code,

function out-default
{
    
}
$PSDefaultParameterValues = @{
    'New-Item:OutVariable' = 'Null'
    'Disabled' = $False
}

function get
{
    return "value"
}

get

# output: value

An alternative way of fixing could be:

<#function out-default
{
    $input | out-null
}#>
$PSDefaultParameterValues = @{
    'New-Item:OutVariable' = 'Null'
    'Disabled' = $False
}

function get
{
    $variable1 = "1"
    $encodingtest = [Text.Encoding]::ASCII.GetBytes( "test" )
    return "value"
}

get

# output: value

In this example I have decided to comment out the first function as it serves no purpose to the script, as you are only calling the get function and never calling out-default function

Marc H
  • 101
  • 5