简体   繁体   中英

How to set a static IP address in windows 10?

After searching a code to set a static IP adress using a simple script, I could not find a complete and easy to implement answer on StackOverflow. That led me to the following question:

What would be an "easy^"-to-implement code to set your windows 10 IP adress to a static IP adress, and back to a dynamic IP adress again?

^ Note: Easy is meant as an indicator to ensure the code and its complete implementation is as simple as possible, not that the user could not find it challenging.

Please note that this is the implementation of: http://www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/ . All credits go to MidnightDBA . I hope it benefits someone!

To set the IP adress to static manually

  1. Start>control panel>Network and Internet>Network and Sharing Center>Change adapter settings>rmb on the ethernet/wifi/connection that is in use>properties>Select: Internet Protocol Version 4(TCP/IPv4)>Properties>

  2. That should result in the screen similar to the attached image. There you can fill in the numbers manually. These numbers will (probably) be different in your own situation, you need to do the work suggested in note 3. to determine those numbers for yourself. 在Windows 10中手动设置IP地址的示例

To set the static IP (semi-automatically):

This means you will be able to to set the IP address to static by opening a file (double clicking a script you've made), and back to a dynamic IP address by running another script you've made. The instruction steps are listed below:

  1. start>type Powershell>rmb>Open powershell as administrator
  2. (Only do this step if you can not immediately run the script the first time.) Type: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser and hit enter, to set the security policy so that you can run a powershell script.
  3. create a .ps1 file named eg static_ip.ps1 in for example c:/example_folder with the content:

     $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableStatic("your_static_ip_adress", "your_subnetmask"); $wmi.SetGateways("your_routers_ip_adress", 1); $wmi.SetDNSServerSearchOrder("your_dns"); 

    OR to set the static IP with just a single double click on the static_ip.ps1 script: (Note example values filled in)

     # 18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP. # First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress: $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if ($testadmin -eq $false) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) exit $LASTEXITCODE } # Next set it static: $wmi.EnableStatic("192.21.89.5", "255.255.254.0"); $wmi.SetGateways("192.21.89.1", 1); $wmi.SetDNSServerSearchOrder("192.21.89.1"); # Now close the window this has just created. # This leaves other Powershell windows open if they were already open before you ran this script. # Also, It yields an error with a $ sign at the start of the line. # Source: https://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit Stop-Process -Id $PID 
  4. Then in powershell enter:

     cd c:/example_folder .\\static_ip.ps1 

    Note, if the path to the static_ip.ps1 file contains a space change the change directory -command to:

     cd "c:/example_folder" 

To make the IP dynamic again (semi-automatically):

  1. Create a text file named for example dynamic_ip.ps1 located eg in folder c:/examplefolder with content:

     $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableDHCP(); $wmi.SetDNSServerSearchOrder(); 

    OR to just change it with a single double-click on the dynamic_ip.ps1 script:

     #18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP. # First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress: $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if ($testadmin -eq $false) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) exit $LASTEXITCODE } # Next set it dynamic: $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableDHCP(); $wmi.SetDNSServerSearchOrder(); # Now close the window this has just created. # This leaves other Powershell windows open if they were already open before you ran this script. # Also, It yields an error with a $ sign at the start of the line. # Source: https://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit Stop-Process -Id $PID 
  2. In powershell:

     cd c:/example_folder .\\dynamic_ip.ps1 

After you have tried it out the first time in powershell succesfully, you can simply set a static IP adress by opening/running the script by opening it with powershell (In explorer, double click the file, or right mouse button (rmb)>open with powershell ). But for this to work, the path to the scripts cannot contain any spaces!

Additional notes:

  1. Do not forget to make the IP adress dynamic again if you leave your home network again, otherwise you can get a problem when you try to access the internet in other wifi/ethernet networks!

  2. your_static_ip_adress : you can read your dynamic ip adress and routers ip adress by: start>type cmd>open command prompt>type: ipconfig , or type: ipconfig -all .* Furthermore, the rules described in the note above, generally apply.

    your_routers_ip_adress see "your_static_ip_adress", usually ends with a .1

    your_subnetmask see "your_static_ip_adress"

    your_dns , this can be your routers ip adress, or for example googles DNS 8.8.8.8 .

  3. Rules to determine the static IP adres: Source: https://www.howtogeek.com/184310/ask-htg-should-i-be-setting-static-ip-addresses-on-my-router/

    3.1 Do not assign an address that ends in .0 or .255 as these addresses are typically reserved for network protocols.

    3.2 Do not assign an address to the very start of the IP pool, eg 10.0.0.1 as the start address is always reserved for the router. Even if you've changed the IP address of your router for security purposes, we'd still suggest against assigning a computer.

    3.3 Do not assign an address outside of the total available pool of private IP addresses. This means if your router's pool is 10.0.0.0 through 10.255.255.255 every IP you assign (keeping in mind the prior two rules) should fall within that range.

  4. This is the (semi) automated equivalent of manually filling in the data of the first figure of this post, in order to set a static IP.

  5. (Wifi connection issues troubleshoot) If:

    • you have 2 different wifi networks ( A and B ) to which you can both connect at the same location
    • where only B has the right "your_routers_ip_adress"/local gateway-adress
    • And you accidentally set your local IP to (the wrong) static IP whilst connect to the wrong wifi ( A ),
    • Then disconnected the wrong wifi ( A ) before setting the local IP adress to dynamic again,
    • and (as a consequence) experience wifi troubles: (keeps scanning network requirements ).

      Then either:

    • set the local IP adress to dynamic .

    • Reconnect to the wrong wifi network ( A ).
    • Set it back to static , and to dynamic again.
    • Disconnect from wifi ( A ).
    • Now you should be able to connect to both wifi networks correct again.

      Or:

    • set the local IP adress to static .

    • Reconnect to the wrong wifi network ( A ).
    • Set it back to static , and to dynamic again.
    • Disconnect from wifi ( A ).
    • Now you should be able to connect to both wifi networks correct again.

Nice information with GUI and PowerShell.

When you assign the IP manually by the PowerShell, the DNS server IP is important. Also, it can be done via the command prompt which is useful while managing the PCs remotely. The below post has the information. Maybe you can consider adding those steps in your post.

https://tinylaptop.net/how-to-configure-setup-static-ip-on-windows-10-laptop/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM