Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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


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.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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
share|improve this answer
Perfect! This is exactly what I've looking for. Thank you! – Junior Dec 23 '09 at 18:59

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.