Add multiple ip's to a windows firewall

Welcome Forums General PowerShell Q&A Add multiple ip's to a windows firewall

This topic contains 1 reply, has 2 voices, and was last updated by

2 years, 2 months ago.

  • Author
  • #64701

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    I'm using the powershell script below to add multiple ip's to a specific firewall rule in this case Rule SMB.

    $name = Get-NetFirewallRule -DisplayName "Rule smb" 
    $ips = @("", "")
    foreach ($r in $name)
    Set-NetFirewallRule -DisplayName $r.DisplayName -RemoteAddress $ips 

    In this case I need to edit the script every time I want to use this. So I thought I'll change the following:
    instead of using

    $ips = @("", "")

    I want to use something like this which I tried

    $ips = @("Read-Host "Enter your server ip's")

    When I run the script i will prompt me to enter the ip addresses I tried to enter, to add both addresses to the rule. But the script fails. I get a error that the address is invalid. Addresses may be specified as ip addresses, ranges etc...
    Also tried to enter ", "" with this the script will fail to.

    the scripts works when I enter only a single ip address so when I want to enter multiple addresses the script fails.
    Can anyone help me with a solution?

    Thanks a lot!


  • #64765

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Read-Host returns a single string. If you typed two IP addresses separated by a comma, that would be the same as:


    So it's not a comma-separated list in a way that PowerShell can recognize and turn into an array. You're going to need a more sophisticated prompting mechanism if that's your goal. Me, I'd probably parameterize the script to accept a [string[]]$addresses parameter, declare it as mandatory, and then let PowerShell prompt. You'd enter one IP address per prompt, and hit Enter on an empty prompt to indicate you were done.

The topic ‘Add multiple ip's to a windows firewall’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort