2

I made a very simple script to enable Remote Desktop connections on a computer:

# Allow Remote Desktop connections
(Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices).SetAllowTsConnections(1)

# Allow low-secutiry connections
(Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace root\cimv2\terminalservices -Filter "TerminalName='RDP-tcp'").SetUserAuthenticationRequired(0)

Now, I wish to finish it, adding a rule for Remote Desktop into Windows Firewall. How I can accomplish this?

BTW: Using Windows 7

Clarification:

Today I enable Remote Desktop manually via "Remote Settings" on "Control Panel\System and Security\System". Doing this way, Windows automatically adds a Firewall Rule to allow incoming remote desktop connections. Fine.

However, using my script above, this last step is not done. On this very configuration screen Windows shows me a reminder message "You must enable the Windows Firewall exception for Remote Desktop"

The question is exactly that: How I can automate this step? Today I do this manually on every computer I install.

4 Answers 4

3

You can use NETSH in PowerShell. To enable the Remote Desktop rule, you can use this command

netsh advfirewall Firewall set rule group="Remote Desktop" new enable=yes
2
  • Perfect! This is exactly what I've looking for. Thank you!
    – Junior
    Dec 23, 2009 at 18:59
  • Thanks for this useful answer. Just to note: The netsh utility can be run in the regular Command Prompt and is not exclusive to PowerShell. Sep 13, 2016 at 14:13
3

As of Windows 8.1 / Server 2012 R2, there are built-in PowerShell cmdlets for managing firewall rules, and netsh advfirewall has a message that it may be removed in a future version of windows.

For this specific case:

Get-NetFirewallRule -DisplayGroup "Remote Desktop" | Set-NetFirewallRule -Enabled True

You can see the rest with:

Get-Help *firewall*

or per the netsh message itself:

Get-Command -Module NetSecurity

Unfortunately, it does not appear that installing WMF 4.0 (PowerShell 4.0) on Windows 7 or Server 2008R2 adds the new NetFirewall cmdlets.

0

Well, I'd have so search more since it isn't obvious to me what you need to enable.

Here's one way to access the local firewalls settings from PowerShell:

PS>$fw=new-object -com HNetCfg.FwMgr

That's using COM, you can also just call the command-line netsh also.

-1

You could also have a look at this Powerpack for Powergui which is for managing the Windows firewall. It might give you some sample code to play with:

Local Firewall Settings PowerPack

You must log in to answer this question.