How can I turn on File and Printer sharing, and the Firewall from the Windows command line

This command should take care of the File and Printer Sharing:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes  

For the firewall:

netsh advfirewall set currentprofile state on  

I've tried them just now, and its seems to work for me.

For any additional information see Microsofts KB article: http://support.microsoft.com/kb/947709


To turn on file and printer sharing only for private profile one can use this command:

netsh firewall set service type=fileandprint mode=enable profile=standard

For Windows 8 or Windows Server 2012 and later OS one can use other command:

wmic /namespace:\\Root\StandardCimv2 path MSFT_NetFirewallRule where (RuleGroup="@FirewallAPI.dll,-28502" and Profiles=2) call enable