Set MTU of interface via powershell
I'm trying to set the MTU for a physical interface programmatically on Windows 7:
PS> (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.Description -match '^Red Hat.*#2' })
DHCPEnabled : False
IPAddress : {10.10.8.3, fe80::447d:38dc:bb39:f311}
DefaultIPGateway :
DNSDomain :
ServiceName : netkvm
Description : Red Hat VirtIO Ethernet Adapter #2
Index : 12
PS> (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.Description -match '^Red Hat.*#2' }).SetMTU(9000)
Method invocation failed because [System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapterConfiguration] doesn't contain a method named 'SetMTU'.
At line:1 char:113
+ (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.Description -match '^Red Hat.*#2' }).SetMTU <<<< (9000)
+ CategoryInfo : InvalidOperation: (SetMTU:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Even though this method exists it still errors? Seriously?
Please help.
PS> (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | \
Where { $_.Description -match '^Red Hat.*#2' }) | Get-Member
returns, among other things:
MTU Property System.UInt32 MTU {get;set;}
But trying to get or set it does nothing:
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | \
Where { $_.Description -match '^Red Hat.*#2' }).MTU
Unless there's an Invoke-Magic
or something I need to do.
As per Ryan's suggestion I had already changed the IPv4 MTU (and IPv6 MTU for good measure):
C:\>netsh interface ipv4 show subinterface "Local Area Connection 2"
MTU MediaSenseState Bytes In Bytes Out Interface
------ --------------- --------- --------- -------------
9000 1 3686 6624 Local Area Connection 2
Seems well and good, but that only affects the subinterface, not the hardware interface:
That's even after a reboot.
Solution 1:
Alright, this doesn't really answer your question, but I guess it contains some decent information anyway, so I'll leave it up. Hopefully someone has a better one.
Do:
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration |
Where { $_.Description -match '^Red Hat.*#2' }) |
Get-Member
And observe the output. You will see that this instance does not actually contain a method named SetMTU, despite what that documentation says. Edit: Actually yours might. But my network interface does not have that. Looks like it's hardware-specific.
So I know what I'm about to do is cheating, but it works:
PS C:\> $AdapterName = $(Get-NetAdapter | Where { $_.Name -Match 'Ethernet'}).Name
PS C:\> netsh interface ipv4 set subinterface "$AdapterName" mtu=1500 store=persistent
Ok.
So like you said, that works for the interface, but maybe not for the hardware NIC. So I haven't truly answered your question.
You also mentioned Set-NetAdapterAdvancedProperty
in your comments as well. However, I do not have an MTU setting there, either. Nor can I set the MTU on the device properties in the Windows GUI. I think the differences are hardware-specific.