4

How can I supress the detailed information from Test-NetConnection -ComputerName localhost for a PowerShell session?

Test-NetConnection writes quite a bit of information to the console which is displayed in the top of the console, overwriting the progress bar.

enter image description here

The output can be suppressed by setting the -InformationLevel to Quiet like so It looked like the output could be supressed like with the -InformationLevel parameter, however it turns out that even with -InformationLevel to Quiet the information in the top of the console is shown.

Test-NetConnection -ComputerName localhost -InformationLevel Quiet

I would like to set the InformationLevel just once for the whole script, like you would suppress the progress bar when using Invoke-WebRequest.

$progressPreference = 'silentlyContinue'    # Subsequent calls do not display UI.
Invoke-WebRequest ...
$progressPreference = 'Continue'            # Subsequent calls do display UI.

To my knowledge there is no similar $InformationLevelPreference Test-NetConnection will listen to.

Jan H
  • 4,287
  • 5
  • 24
  • 34

2 Answers2

8

An alternative to Ansgar's proposed solution would be to use the $PSDefaultParameterValues automatic variable:

PS C:\> Test-NetConnection localhost

ComputerName           : localhost
RemoteAddress          : ::1
InterfaceAlias         : Loopback Pseudo-Interface 1
SourceAddress          : ::1
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms

PS C:\> $PSDefaultParameterValues['Test-NetConnection:InformationLevel'] = 'Quiet'
PS C:\> Test-NetConnection localhost
True

Entries in $PSDefaultParameterValues dictionary have keys in the form of CommandName:ParameterName, and then the value is the parameter argument value you want to supply by default. You can incorporate wildcards as part of the CommandName part if necessary


The reason that you might have seen the progress bar go away when switching to -InformationLevel:Quiet is that issuing a single ICMP request to a loopback interface and returning a single boolean value happens so fast that the host application never has a chance to actually pick up the progress stream state change and display the progress overlay before returning to the prompt.

If you want to suppress the progress bar at the top during execution (regardless of how long the command is running), use the $ProgressPreference variable:

PS C:\> $ProgressPreference = 'SilentlyContinue'
PS C:\> Test-NetConnection localhost
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • 1
    Nice one. I wasn't aware of this. – Ansgar Wiechers Sep 14 '17 at 09:51
  • I did not know about `$PSDefaultParameterValues` either. I did find strange behaviour. Your solution works fine for `Test-NetConnection -ComputerName localhost`, but it it does not work with `Test-NetConnection -ComputerName localhost -Port 80`! – Jan H Sep 14 '17 at 11:46
  • @JanH Interesting - it works just fine here, what output are you getting other than `$true/$false`? – Mathias R. Jessen Sep 14 '17 at 11:53
  • 1
    Actually, withe the `-Port` parameter it works as expected. Using `PSDefaultParameterValues` renders the same behavior as using `Test-NetConnection localhost -Port 80 -InformationLevel Quiet` – Jan H Sep 14 '17 at 13:49
  • In the question I stated that `Test-NetConnection -ComputerName localhost -InformationLevel Quiet` suppresses the information shown on top of the window, but it does not. I think I have to rephrase the question. – Jan H Sep 14 '17 at 13:51
  • @JanH Sounds like you're trying to supress progress information being displayed by the host. There's a preference variable for that :-) I've updated the answer – Mathias R. Jessen Sep 14 '17 at 14:03
  • @MathiasR.Jessen Thanks for the suggestion! Somehow with `Test-NetConnection` the `$ProgressPreference = 'SilentlyContinue` only works if you set it in the console itself. When setting the progresspreference in a script it does not work. – Jan H Sep 15 '17 at 08:30
  • @JanH Try setting `$global:ProgressPreference`. – Ansgar Wiechers Sep 15 '17 at 09:54
  • @AnsgarWiechers brilliant! that totally solves my problem. And yeah, I should read up on PowerShell variable scopes again. Thanks! – Jan H Sep 16 '17 at 19:29
3

Wrap Test-NetConnection in a function and use a global variable for your preference.

$NetConnectionVerbosity = 'Quiet'

function Test-CustomNetConnection {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ComputerName = 'localhost',

        [Parameter(Mandatory=$false)]
        [string]$InformationLevel = $script:NetConnectionVerbosity
    )

    Test-NetConnection -ComputerName $ComputerName -InformationLevel $InformationLevel
}

Test-CustomNetConnection

You could supersede Test-NetConnection with a custom function and an alias:

$NetConnectionVerbosity = 'Quiet'

function Test-CustomNetConnection {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ComputerName = 'localhost',

        [Parameter(Mandatory=$false)]
        [string]$InformationLevel = $script:NetConnectionVerbosity
    )

    $f = Get-Command -Name 'Test-NetConnection' -CommandType Function
    & $f -ComputerName $ComputerName -InformationLevel $InformationLevel
}

New-Alias -Name 'Test-NetConnection' -Value 'Test-CustomNetConnection'

Test-NetConnection

The above defines a custom function that gets and invokes the original Test-NetConnection function, then defines an alias Test-NetConnection for the custom function. Since aliases take precedence over functions invocations of Test-NetConnection calls will now invoke Test-CustomNetConnection, which in turn invokes the original Test-NetConnection with the desired parameters.

Creating a proxy function might also be an option.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Good idea. In my case there is a 3rd party installation script which uses `Test-NetConnection`. I cannot change that script so I am looking for a solution where I can change the behavior of `Test-NetConnection` in another way. I do not want to supress the output of the installation script completely either because it has some valuable information. – Jan H Sep 14 '17 at 08:41