2

This command doesn't work:

netsh advfirewall set allprofiles firewallpolicy blockinboundalways,allowoutbound

This command works:

netsh advfirewall set allprofiles state on

I've tried using invoke-command and &, but I could not get it to work. When I run the above command in PS, it tells me:

The number of arguments provided is not valid. Check help for the correct syntax.

  • Use PS v5 or escape ,: netsh advfirewall set allprofiles firewallpolicy blockinboundalways`,allowoutbound. – user364455 Dec 8 '15 at 2:54
  • @PetSerAl Thanks, that works perfectly. Is there a reason why I need the escape character in this specific command and not any others? My script contains maybe 30-40 different types of commands and this is the first time I've needed to use an escape character outside of a string. – RMal Dec 8 '15 at 3:20
  • You need to escape comma, because outside of string it is special character in PowerShell. Possible, it is your first case, when you need to pass literal comma to native application. – user364455 Dec 8 '15 at 3:47
7

Comma is a special character for PowerShell. In your case it is interpreted as a binary array operator. It creates array with two elements blockinboundalways and allowoutbound. PowerShell below v5 will separate array elements with space when array passed to native applications. The resulting command line passed to netsh will be following:

netsh advfirewall set allprofiles firewallpolicy blockinboundalways allowoutbound

Which is not what you want. You have to escape comma or enclose it in quotes to interpret it literally:

netsh advfirewall set allprofiles firewallpolicy blockinboundalways`,allowoutbound
netsh advfirewall set allprofiles firewallpolicy 'blockinboundalways,allowoutbound'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.