{"id":2230,"date":"2014-01-14T00:01:00","date_gmt":"2014-01-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/14\/renaming-network-adapters-by-using-powershell\/"},"modified":"2014-01-14T00:01:00","modified_gmt":"2014-01-14T00:01:00","slug":"renaming-network-adapters-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/renaming-network-adapters-by-using-powershell\/","title":{"rendered":"Renaming Network Adapters by Using PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about various ways to rename network adapters by using Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a cup of tea made from English Breakfast, peppermint and spearmint leaves, strawberry leaves, licorice root, and a cinnamon stick. It is delightfully refreshing. I am working on Windows PowerShell Saturday #007 that will be in Charlotte.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1184.Sat007.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1184.Sat007.png\" alt=\"Image of logo\" title=\"Image of logo\" \/><\/a><\/p>\n<p><a href=\"http:\/\/powershellsaturday.com\/007\/conference\/powershell-saturday-007-charlotte-nc-february-8-2014\/\" target=\"_blank\">PowerShell Saturday #007<\/a> will be held in Charlotte, North Carolina on February 8, 2014. This will be an awesome chance to meet and to learn from some of the best PowerShellers around. In fact, five of the speakers are also speakers at the PowerShell Summit this year. There are a limited number of tickets still available for this event, so you&rsquo;ll want to sign up now. The Scripting Wife wrote a great post that provides a quick overview of the event: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/psst-charlotte-powershell-saturday-details-leaked\/\" target=\"_blank\">Psst&#8230;Charlotte PowerShell Saturday Details Leaked<\/a>.<\/p>\n<p>Now let&rsquo;s visit how to rename network adapters&hellip;<\/p>\n<p><b>&nbsp; &nbsp; &nbsp;Note<\/b>&nbsp; This is the third post in a series that examines working with network adapters. You may want to refer to <br \/>&nbsp; &nbsp; &nbsp;the earlier posts to get a feel for the way the series progresses:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-identify-network-adapter-characteristics\/\" target=\"_blank\">Use PowerShell to Identify Network Adapter Characteristics<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/enabling-and-disabling-network-adapters-with-powershell\/\" target=\"_blank\">Enabling and Disabling Network Adapters with PowerShell<\/a><\/li>\n<\/ul>\n<p>Depending on the version of your operating system, you will have various capabilities available for renaming the network adapter. These methods involve using Netsh, WMI, and the functions from the <b>NetAdapter<\/b> module.<\/p>\n<h2>Using Netsh<\/h2>\n<p>To rename the network adapter by using Netsh, I need to know the interface name and the new name I want to use. That is about it. To find the network adapter names, I can also use Netsh. Here is the command:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv4 show interfaces<\/p>\n<p>Netsh is available everywhere right now. I can use Netsh to configure network adapters from Windows 2000 forward, so it has the greatest amount of backward compatibility. But it is being deprecated, and therefore, it may not always be available going forward. To rename a network interface by using Netsh, I can use a command such as the following:<\/p>\n<p style=\"margin-left:30px\">Netsh interface set interface name=&quot;Ethernet&quot; newname=&quot;RenamedAdapter&quot;<\/p>\n<h2>Using WMI<\/h2>\n<p>Since Windows Vista, it has been possible to use WMI to rename the network interface. The thing to keep in mind is that the property I change is <b>NetConnectionID<\/b> and not the <b>Name<\/b> property. Because this command modified the <b>NetConnectionID<\/b> property, it is a simple property assignment, and not a method call.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394216%28v=vs.85%29.aspx\" target=\"_blank\">Win32_NetworkAdapter WMI class<\/a> shows the properties that are Read and Write. The steps to using WMI include:<\/p>\n<ol>\n<li>Retrieve the specific instance of the network adapter.<\/li>\n<li>Assign a new value for the <b>NetConnectionID<\/b> property.<\/li>\n<li>Use the <b>Put<\/b> method to write the change back to WMI.<\/li>\n<\/ol>\n<p>The following command illustrates these steps using a network adapter that is named Ethernet. The command will rename the network adapter named Ethernet to RenamedConnection:<\/p>\n<p style=\"margin-left:30px\">$wmi = Get-WmiObject -Class Win32_NetworkAdapter -Filter &quot;NetConnectionID = &#039;Ethernet&#039;&quot;<\/p>\n<p style=\"margin-left:30px\">$wmi.NetConnectionID = &#039;RenamedConnection&#039;<\/p>\n<p style=\"margin-left:30px\">$wmi.Put()<\/p>\n<p>The following image shows using WMI to rename the network adapter.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Using WMI in Windows 7<\/h2>\n<p>In Windows 7 and Windows Server&nbsp;2008&nbsp;R2, it is not necessary to use the <b>Get-WmiObject<\/b> cmdlet, assign new values for the property, and call the <b>Put<\/b> method. This is because the <b>Set-CimInstance <\/b>cmdlet permits accomplishing this feat as single command.<\/p>\n<p>The easiest way to use <b>Set-CimInstance<\/b> is to use a query. Interestingly enough, this WQL query is the same type of query that would have been used back in the VBScript days. The query to retrieve the network adapter named Ethernet is shown here:<\/p>\n<p style=\"margin-left:30px\">&quot;Select * from Win32_NetworkAdapter where NetConnectionID = &#039;EtherNet&#039;&quot;<\/p>\n<p>To assign a new value for a property, I use a hash table. The hash table specifies the property and the new value for the property. The hash table to specify a value of <b>RenamedConnection<\/b> for the <b>NetConnectionID<\/b> property is shown here:<\/p>\n<p style=\"margin-left:30px\">@{NetConnectionID=&quot;RenamedConnection&quot;}<\/p>\n<p>The complete Set-CimInstance command appears here (this is a single line command)<\/p>\n<p style=\"margin-left:30px\">Set-CimInstance -Query &quot;Select * from Win32_NetworkAdapter where NetConnectionID = &#039;EtherNet&#039;&quot; -Property @{NetConnectionID=&quot;RenamedConnection&quot;}<\/p>\n<p>When I run the command, nothing appears in the output. The following image shows the single command (wrapping in the Windows PowerShell console) and no output from the command. On my laptop running Windows&nbsp;8.1, I use the <b>Get-NetAdapter<\/b> cmdlet to verify that the adapter renamed.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Using the NetAdapter module<\/h2>\n<p>Renaming a network adapter via Windows PowerShell requires admin rights. Unfortunately, the Help does not mention this. You just have to know this. Luckily, an error message appears when attempting to run the command without admin rights. The message is instructive, and it informs you that access is denied. It is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-03.png\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<p>The good thing is that the access denied<i> <\/i>error message appears. Some cmdlets do not display output, and they do not let you know that you need admin rights to obtain the information. The <b>Get-VM<\/b> cmdlet is one of those&mdash;it returns no virtual machine information, but it does not generate an error message either. This situation is also true with the <b>Start-VM<\/b> cmdlet&mdash;it does not do anything, but it does not generate an error message if you do not have the proper rights.<\/p>\n<p>So I close the Windows PowerShell console. I right-click the Windows PowerShell console icon that I created on my task bar, and run Windows PowerShell as Administrator. I now run the command to rename my network adapter with the <b>whatif<\/b><i> <\/i>parameter to ensure that it accomplishes what I want. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -Name Ethernet | Rename-NetAdapter -NewName Renamed -whatif<\/p>\n<p>What if: Rename-NetAdapter -Name &#039;Ethernet&#039; -NewName &#039;Renamed&#039;<\/p>\n<p>That is exactly what I want to happen. I now use the Up arrow, and remove the <b>whatif <\/b>parameter<i>. <\/i>Here is the command (no output returns from this command).<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -Name Ethernet | Rename-NetAdapter -NewName Renamed<\/p>\n<p>The command and its associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can modify my command a bit, and return an instance of the renamed network adapter. To do this, I use the <b>&ndash;passthru<\/b> parameter from the <b>Rename-NetAdapter<\/b> function. One reason to do this is to see visual confirmation that the command completed successfully. Other reasons would be to use the returned object to feed into other cmdlets and to perform other actions. Here is the revised command, which shows how to use <b>&ndash;passthru<\/b>:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -Name Ethernet | Rename-NetAdapter -NewName Renamed &ndash;PassThru<\/p>\n<p>The command and its associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the really powerful things about the <b>Get-NetAdapter<\/b> function is that I can use wildcard characters with the <b>Name<\/b> parameter. This means that if I do not want to type the entire network adapter name, I can shorten it. It also means that if I have a similar naming pattern, I can also use a wildcard pattern to retrieve them. Here is an example of using a wildcard character:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -Name Ether*<\/p>\n<p>This command works the same as the other commands, and therefore I can pipe the results to the <b>Rename-NetAdapter<\/b> function. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -Name Ether* | Rename-NetAdapter -NewName Renamed &ndash;PassThru<\/p>\n<p>As shown in the following image, the command works perfectly:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-14-14-06.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Network Adapter Week will continue tomorrow when I will talk about how to find connected network adapters.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about various ways to rename network adapters by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a cup of tea made from English Breakfast, peppermint and spearmint leaves, strawberry leaves, licorice root, and a cinnamon stick. It is delightfully refreshing. I [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[34,478,3,45],"class_list":["post-2230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hardware","tag-network-adapter","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about various ways to rename network adapters by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a cup of tea made from English Breakfast, peppermint and spearmint leaves, strawberry leaves, licorice root, and a cinnamon stick. It is delightfully refreshing. I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2230","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2230"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2230\/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=2230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}