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:

New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow

Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'

Ideally, I would like the output to simply be:

Windows Firewall configured to allow incoming connections on TCP port 1433

Instead, I’m getting:

Caption                 :
Description             :
ElementName             : MSSQL ENGINE TCP
InstanceID              : {752a3c18-298f-4639-a462-4cc5205b1016}
CommonName              :
PolicyKeywords          :
Enabled                 : True
PolicyDecisionStrategy  : 2
PolicyRoles             :
ConditionListType       : 3
CreationClassName       : MSFT|FW|FirewallRule|{752a3c18-298f-4639-a462- 
4cc5205b1016}
ExecutionStrategy       : 2
Mandatory               :
PolicyRuleName          :
Priority                :
RuleUsage               :
SequencedActions        : 3
SystemCreationClassName :
SystemName              :
Action                  : Allow
Direction               : Inbound
DisplayGroup            :
DisplayName             : MSSQL ENGINE TCP
EdgeTraversalPolicy     : Block
EnforcementStatus       : NotApplicable
LocalOnlyMapping        : False
LooseSourceMapping      : False
Owner                   :
Platforms               : {}
PolicyStoreSource       : PersistentStore
PolicyStoreSourceType   : Local
PrimaryStatus           : OK
Profiles                : 0
RuleGroup               :
Status                  : The rule was parsed successfully from the store. (65536)
StatusCode              : 65536
PSComputerName          :
Name                    : {752a3c18-298f-4639-a462-4cc5205b1016}
ID                      : {752a3c18-298f-4639-a462-4cc5205b1016}
Group                   :
Platform                : {}
LSM                     : False
Profile                 : Any

Windows Firewall configured to allow incoming connections on TCP port 1433

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:

$null = New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
Write-Host 'Windows Firewall configured to allow incoming connections on TCP port 1433'

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.

[void](New-NetFirewallRule -DisplayName "MSSQL ENGINE TCP" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow)

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:

$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$list.add($_) | Out-Null} } | Select-Object -Property ticks,totalmilliseconds

  Ticks TotalMilliseconds
  ----- -----------------
6354765          635.4765

[void]:

$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {[void]$list.add($_)} } | Select-Object -Property ticks,totalmilliseconds

  Ticks TotalMilliseconds
  ----- -----------------
1323269          132.3269

$null:

$list = @() -as [system.collections.arraylist]
measure-command {(1..10000) | Foreach-Object {$null = $list.add($_)} } | Select-Object -Property  ticks,totalmilliseconds

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