Skip to content
Advertisement

Change Windows Firewall Settings With PowerShell But Do Not Show Output

I have a PowerShell script I am using to install SQL Express, then SQL Server Management Studio, and finally, to edit the Windows Firewall settings to allow remote connections to the database. For the firewall changes, one of the lines I’m running is:

Ideally, I would like the output to simply be:

Instead, I’m getting:

Is there an easy way to eliminate all the excess from showing up in the PowerShell window? I know I could create a second script and prompt that to run in a separate window, but I’m trying to accomplish this with a single script.

Advertisement

Answer

The following will suppress command output and still execute the command:

When an output is saved to $null, the output is removed.

You can also cast to [void], which in certain cases may yield better performance than assigning to $null.

It is probably going to be negligible performance-wise with either case. You should avoid using Out-Null in all cases because that will always be slower.

It is generally not recommended to use Write-Host, but since I don’t know how you are calling or executing your code, I am leaving that there. If you are executing this within the PowerShell console, you can simply just leave the quoted text on a line by itself.

Here are some performance tests to compare the three methods:

Out-Null:

[void]:

$null:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement