{"id":67653,"date":"2006-03-29T15:38:00","date_gmt":"2006-03-29T15:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/29\/how-can-i-change-the-ip-address-assigned-to-a-computer\/"},"modified":"2006-03-29T15:38:00","modified_gmt":"2006-03-29T15:38:00","slug":"how-can-i-change-the-ip-address-assigned-to-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-ip-address-assigned-to-a-computer\/","title":{"rendered":"How Can I Change the IP Address Assigned to a Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I change the IP address assigned to a computer?<BR><BR>&#8212; RW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RW. To tell you the truth, this is a very difficult question for the Scripting Guys to answer. Not because this is a hard script to write; as you\u2019ll see in a minute, it\u2019s actually pretty easy. No, this is hard for the Scripting Guys because it involves change, and when you\u2019re a Scripting Guy, well, why would you ever want to change <I>anything<\/I>? As you might expect, we all drive imported sports cars, we all live in opulent mansions, and we all light our cigars with $100 bills. Do we have the perfect lives? You bet we do: even our IP addresses are perfect. Why would we want to change any of that, IP addresses included?<\/P>\n<TABLE class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. OK, we admit it: we may have exaggerated a little here. Sometimes &#8211; and we\u2019re a bit embarrassed to admit this &#8211; we light our cigars using $10 bills. The Scripting Guys deeply regret any inconvenience our previous statements might have caused.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, unlike most Microsoft people the Scripting Guys aren\u2019t <I>totally<\/I> out-of-touch with the real world: we realize that there are a few people here and there who don\u2019t have several million dollars in their bank account. (Not that we\u2019ve met any of these people, mind you, but we saw some of them on TV. Or at least we think we did; we quickly switched the channel to <I>Lifestyles of the Rich and Famous Scripters<\/I>.) That means that, for some people, change is good. And it goes without saying that changing their IP address is even better:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True&#8221;)<\/p>\n<p>strIPAddress = Array(&#8220;192.168.1.0&#8221;)\nstrSubnetMask = Array(&#8220;255.255.255.0&#8221;)<\/p>\n<p>For Each objNetAdapter in colNetAdapters\n    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)\nNext\n<\/PRE>\n<P>Before we explain how this script works we should issue one important caveat. We\u2019re assuming (based simply on the fact that you didn\u2019t say otherwise) that all you need to do is change the IP address; in other words, we\u2019re assuming that you don\u2019t need to change your IP gateway, that you don\u2019t need to change your DNS servers, that you don\u2019t need to change your WINS servers, etc. All of those things (and more) can be modified using a script, but it would take a few hundred pages for us to detail all the possibilities. And we\u2019d be willing to do that, too, except that the legendary Peter Costantini has already done it for us; check out Peter\u2019s classic tome <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/networking\/default.mspx\"><B>Automating TCP\/IP Networking on Clients<\/B><\/A> for more information.<\/P>\n<P>And no, although he could have, Peter didn\u2019t hire any consultants: he came up with that title all by himself. <\/P>\n<P>Really.<\/P>\n<P>Now, back to the business at hand. Like we said, this is a pretty simple little script. We start out by connecting to the WMI service on the local computer, although we could use this same script to assign a new IP address to a remote machine. (How? Just assign the name of the remote machine to the variable strComputer.) We then use this line of code to return a collection of all the IP-enabled network adapters on the computer:<\/P><PRE class=\"codeSample\">Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True&#8221;)\n<\/PRE>\n<P>Here\u2019s another caveat. This script assumes you have only one IP-enabled network adapter on the computer. In this day-and-age, however, it\u2019s very possible for you to have more than one network adapter installed in a machine. If that\u2019s the case then you should modify this query to return just the network adapter that\u2019s supposed to get the new IP address. How do you do that? Well, an easy way is to specify the adapter <B>Description<\/B> in the query:<\/P><PRE class=\"codeSample\">Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where Description = &#8221; &amp; _\n        &#8220;&#8216;Broadcom NetLink (TM) Gigabit Ethernet'&#8221;)\n<\/PRE>\n<P>So couldn\u2019t we have used the Description property in our query to begin with? Sure, it\u2019s just that we haven\u2019t memorized the descriptions for our network adapters. (Good point: what <I>were<\/I> we thinking?) Because of that it was easier for us to use <B>IPEnabled=True<\/B>. Like we said, though, that won\u2019t work if you have more than one IP-enabled network adapter installed. You\u2019ll have to figure out what works best for you.<\/P>\n<TABLE class=\"dataTable\" id=\"EKE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Scripting Guys Tip<\/B>. When you\u2019re trying to figure this out, we recommend that you write the query and then test it by having the script do something harmless, like echo back the Description for each item in the collection. Don\u2019t actually start changing IP addresses until you know for sure which adapters will be affected by the script and which ones won\u2019t.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, where were we? Oh, right: these two lines of code:<\/P><PRE class=\"codeSample\">strIPAddress = Array(&#8220;192.168.1.0&#8221;)\nstrSubnetMask = Array(&#8220;255.255.255.0&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is assigning the new IP address (192.168.1.0) to a variable named strIPAddress and the corresponding subnet mask (255.255.255.0) to a variable named strSubnetMask. The tricky part is that we have to assign these values as arrays; hence the use of the <B>Array<\/B> function. And, yes, we know that we have only one IP address and one subnet mask. That doesn\u2019t matter; we still have to configure these as arrays or the script will fail.<\/P>\n<P>After setting up our variables we have just one thing left to do: actually assign the new IP address and subnet mask. To do that we set up a For Each loop to loop through the collection of network adapters (see why it\u2019s important that our collection have only one such adapter in it?) and then call the <B>EnableStatic<\/B> method to assign the new IP address:<\/P><PRE class=\"codeSample\">For Each objNetAdapter in colNetAdapters\n    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)\nNext\n<\/PRE>\n<P>Yes, very simple: all we do is pass EnableStatic our two variables (strIPAddress and strSubnetMask) and we\u2019re done.<\/P>\n<P>Like we said, there are plenty of other network adapter settings that can be changed using a script. However, we\u2019ll have to refer you to Peter\u2019s <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/networking\/default.mspx\"><B>white paper<\/B><\/A> for information on that. Right now we need to call room service and order another tray of cigars and a stack of $100 bills.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the IP address assigned to a computer?&#8212; RW Hey, RW. To tell you the truth, this is a very difficult question for the Scripting Guys to answer. Not because this is a hard script to write; as you\u2019ll see in a minute, it\u2019s actually pretty easy. No, this [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,37,3,5],"class_list":["post-67653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change the IP address assigned to a computer?&#8212; RW Hey, RW. To tell you the truth, this is a very difficult question for the Scripting Guys to answer. Not because this is a hard script to write; as you\u2019ll see in a minute, it\u2019s actually pretty easy. No, this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}