Created
June 15, 2025 14:17
-
-
Save renaudcerrato/99e423b84e1c0200793467dedad2a0f7 to your computer and use it in GitHub Desktop.
Powershell script to set NIC properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -Modules NetAdapter | |
| #Requires -RunAsAdministrator | |
| <# | |
| .SYNOPSIS | |
| Conditionally sets or displays a specified advanced property of a network adapter. | |
| Filters adapters by keyword support. If no adapter name is given and a single | |
| matching adapter is found, it's selected automatically. Otherwise, it prompts. | |
| .DESCRIPTION | |
| This script can operate in two modes: | |
| 1. Set Mode (if TargetValue is provided): | |
| Checks the current value of a specified registry keyword (advanced property) | |
| for a given network adapter. If the current value is different from the TargetValue, | |
| the script attempts to set it. | |
| 2. Display Mode (if TargetValue is NOT provided): | |
| Shows the current DisplayValue and RegistryValue of the specified RegistryKeyword | |
| for the identified network adapter. | |
| Adapter Identification: | |
| - If the AdapterName parameter is omitted, the script will: | |
| 1. Find all network adapters that support the specified RegistryKeyword. | |
| 2. If exactly one such adapter is found, it will be automatically selected. | |
| 3. If multiple such adapters are found, the user will be prompted to choose one. | |
| 4. If no such adapters are found, an error will be reported. | |
| - If the AdapterName parameter is provided, the script will first verify that | |
| the specified adapter supports the RegistryKeyword. | |
| The RegistryKeyword parameter is mandatory. | |
| It requires administrative privileges to run. | |
| In Set Mode, the -NoRestart switch is NOT used, so an adapter might restart if the change requires it. | |
| .PARAMETER AdapterName | |
| The name of the network adapter to configure or query. | |
| If omitted, the script will attempt to find and select an adapter automatically or prompt if multiple are found. | |
| .PARAMETER RegistryKeyword | |
| The registry keyword for the advanced property to configure or query (e.g., "*SpeedDuplex"). | |
| This parameter is mandatory. The script will only consider adapters supporting this keyword. | |
| .PARAMETER TargetValue | |
| The desired value for the specified advanced property. | |
| If provided, the script operates in "Set Mode". | |
| If omitted, the script operates in "Display Mode" and shows the current property value. | |
| .EXAMPLE | |
| # Set Mode: Set *SpeedDuplex to 6 for adapter "Thermal" | |
| .\Set-NetAdapterAdvancedPropertyConditionally.ps1 -AdapterName "Thermal" -RegistryKeyword "*SpeedDuplex" -TargetValue "6" | |
| .EXAMPLE | |
| # Set Mode: Auto-select/prompt for adapter supporting *SpeedDuplex and set to 6 | |
| .\Set-NetAdapterAdvancedPropertyConditionally.ps1 -RegistryKeyword "*SpeedDuplex" -TargetValue "6" | |
| .EXAMPLE | |
| # Display Mode: Show current *SpeedDuplex value for adapter "Thermal" | |
| .\Set-NetAdapterAdvancedPropertyConditionally.ps1 -AdapterName "Thermal" -RegistryKeyword "*SpeedDuplex" | |
| .EXAMPLE | |
| # Display Mode: Auto-select/prompt for adapter supporting *SpeedDuplex and show its value | |
| .\Set-NetAdapterAdvancedPropertyConditionally.ps1 -RegistryKeyword "*SpeedDuplex" | |
| .NOTES | |
| The script uses Get-NetAdapter, Get-NetAdapterAdvancedProperty, and Set-NetAdapterAdvancedProperty, | |
| which are part of the NetAdapter module. | |
| Ensure PowerShell is run as Administrator. | |
| In Set Mode, changes to network adapter properties might cause a brief network interruption or require an adapter restart. | |
| #> | |
| param ( | |
| [string]$AdapterName, | |
| [Parameter(Mandatory=$true)] | |
| [string]$RegistryKeyword, | |
| [string]$TargetValue # No longer mandatory | |
| ) | |
| # Check if running as Administrator first | |
| if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
| Write-Error "This script must be run as Administrator." | |
| Exit 1 | |
| } | |
| # --- Adapter Identification and Validation --- | |
| # This section ensures $AdapterName is populated with a valid adapter that supports $RegistryKeyword, or the script exits. | |
| if ([string]::IsNullOrWhiteSpace($AdapterName)) { | |
| Write-Host "AdapterName not specified. Searching for adapters that support '$RegistryKeyword'..." | |
| $AllAdapters = @() | |
| try { | |
| $AllAdapters = Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, InterfaceIndex -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Error "Failed to retrieve network adapters. Error: $($_.Exception.Message)" | |
| Exit 1 | |
| } | |
| if ($AllAdapters.Count -eq 0) { | |
| Write-Error "No network adapters found on this system." | |
| Exit 1 | |
| } | |
| $SupportedAdapters = [System.Collections.Generic.List[object]]::new() | |
| Write-Host "Checking which adapters support '$RegistryKeyword'..." | |
| foreach ($AdapterPnPInfo in $AllAdapters) { | |
| try { | |
| Get-NetAdapterAdvancedProperty -Name $AdapterPnPInfo.Name -RegistryKeyword $RegistryKeyword -ErrorAction Stop | Out-Null | |
| $SupportedAdapters.Add($AdapterPnPInfo) | |
| Write-Verbose "Adapter '$($AdapterPnPInfo.Name)' supports '$RegistryKeyword'." | |
| } | |
| catch { | |
| Write-Verbose "Adapter '$($AdapterPnPInfo.Name)' does not support '$RegistryKeyword' or an error occurred: $($_.Exception.Message)" | |
| } | |
| } | |
| if ($SupportedAdapters.Count -eq 0) { | |
| Write-Error "No network adapters found that support the registry keyword '$RegistryKeyword'." | |
| Exit 1 | |
| } | |
| elseif ($SupportedAdapters.Count -eq 1) { | |
| $AdapterName = $SupportedAdapters[0].Name | |
| Write-Host "Automatically selected adapter '$AdapterName' as it's the only one supporting '$RegistryKeyword'." | |
| } | |
| else { # Multiple supported adapters, prompt user | |
| Write-Host "The following adapters support '$RegistryKeyword':" | |
| for ($i = 0; $i -lt $SupportedAdapters.Count; $i++) { | |
| Write-Host ("{0,3}: {1,-30} (Description: {2})" -f ($i + 1), $SupportedAdapters[$i].Name, $SupportedAdapters[$i].InterfaceDescription) | |
| } | |
| [int]$Choice = 0 | |
| while ($Choice -lt 1 -or $Choice -gt $SupportedAdapters.Count) { | |
| try { | |
| $Input = Read-Host -Prompt "Enter the number of the adapter to configure/query (1-$($SupportedAdapters.Count))" | |
| $Choice = [int]$Input | |
| if ($Choice -lt 1 -or $Choice -gt $SupportedAdapters.Count) { | |
| Write-Warning "Invalid selection. Please enter a number between 1 and $($SupportedAdapters.Count)." | |
| } | |
| } | |
| catch { | |
| Write-Warning "Invalid input. Please enter a valid number." | |
| } | |
| } | |
| $AdapterName = $SupportedAdapters[$Choice - 1].Name | |
| Write-Host ("You selected adapter: '{0}'" -f $AdapterName) | |
| } | |
| } | |
| else { | |
| # AdapterName IS provided, verify it exists and supports the keyword | |
| Write-Host "Verifying if specified adapter '$AdapterName' supports registry keyword '$RegistryKeyword'..." | |
| $AdapterExists = $false | |
| try { | |
| Get-NetAdapter -Name $AdapterName -ErrorAction Stop | Out-Null | |
| $AdapterExists = $true | |
| } | |
| catch { | |
| Write-Error "The specified network adapter '$AdapterName' was not found. Error: $($_.Exception.Message)" | |
| Exit 1 | |
| } | |
| if ($AdapterExists) { | |
| try { | |
| Get-NetAdapterAdvancedProperty -Name $AdapterName -RegistryKeyword $RegistryKeyword -ErrorAction Stop | Out-Null | |
| Write-Host "Adapter '$AdapterName' supports '$RegistryKeyword'." | |
| } | |
| catch { | |
| Write-Error "The specified adapter '$AdapterName' does not support the registry keyword '$RegistryKeyword'. Error: $($_.Exception.Message)" | |
| Exit 1 | |
| } | |
| } | |
| } | |
| # --- Operation Mode: Display or Set --- | |
| if ([string]::IsNullOrWhiteSpace($TargetValue)) { | |
| # --- Display Mode --- | |
| Write-Host "" # Newline for better formatting | |
| Write-Host "Operating in Display Mode for adapter '$AdapterName', keyword '$RegistryKeyword'." | |
| try { | |
| $PropertyInfo = Get-NetAdapterAdvancedProperty -Name $AdapterName -RegistryKeyword $RegistryKeyword -ErrorAction Stop | |
| Write-Host "--------------------------------------------------" | |
| Write-Host "Property Details for Adapter: $($PropertyInfo.Name)" | |
| Write-Host "--------------------------------------------------" | |
| Write-Host "Registry Keyword : $($PropertyInfo.RegistryKeyword)" | |
| Write-Host "Display Name : $($PropertyInfo.DisplayName)" | |
| Write-Host "Display Value : $($PropertyInfo.DisplayValue)" | |
| $CurrentRegValue = "<not set or N/A>" | |
| if ($null -ne $PropertyInfo.RegistryValue -and $PropertyInfo.RegistryValue.Count -gt 0) { | |
| $CurrentRegValue = if ($PropertyInfo.RegistryValue.Count -gt 1) { $PropertyInfo.RegistryValue -join ", " } else { $PropertyInfo.RegistryValue[0] } | |
| } | |
| Write-Host "Registry Value(s): $CurrentRegValue" | |
| Write-Host "--------------------------------------------------" | |
| } | |
| catch { | |
| Write-Error "Failed to retrieve advanced property '$RegistryKeyword' for adapter '$AdapterName'. Error: $($_.Exception.Message)" | |
| if ($_.FullyQualifiedErrorId) { | |
| Write-Error "Fully Qualified Error ID: $($_.FullyQualifiedErrorId)" | |
| } | |
| } | |
| } | |
| else { | |
| # --- Set Mode --- | |
| Write-Host "Operating in Set Mode." | |
| Write-Host "Attempting to configure advanced property '$RegistryKeyword' for adapter '$AdapterName' to value '$TargetValue'..." | |
| try { | |
| Write-Host "Fetching current '$RegistryKeyword' value for adapter '$AdapterName'..." | |
| $CurrentProperty = Get-NetAdapterAdvancedProperty -Name $AdapterName -RegistryKeyword $RegistryKeyword -ErrorAction Stop | |
| $CurrentActualValue = $null | |
| if ($null -ne $CurrentProperty.RegistryValue -and $CurrentProperty.RegistryValue.Count -gt 0) { | |
| $CurrentActualValue = $CurrentProperty.RegistryValue[0] | |
| } | |
| Write-Host "Current '$RegistryKeyword' registry value: '$($CurrentActualValue)'" | |
| if ($CurrentActualValue -ne $TargetValue) { | |
| Write-Host "Current registry value ('$CurrentActualValue') is different from target value ('$TargetValue')." | |
| Write-Host "Setting '$RegistryKeyword' to '$TargetValue' for adapter '$AdapterName'..." | |
| Set-NetAdapterAdvancedProperty -Name $AdapterName -RegistryKeyword $RegistryKeyword -RegistryValue $TargetValue -ErrorAction Stop | |
| $NewProperty = Get-NetAdapterAdvancedProperty -Name $AdapterName -RegistryKeyword $RegistryKeyword | |
| $NewActualSetValue = $null | |
| if ($null -ne $NewProperty.RegistryValue -and $NewProperty.RegistryValue.Count -gt 0) { | |
| $NewActualSetValue = $NewProperty.RegistryValue[0] | |
| } | |
| if ($NewActualSetValue -eq $TargetValue) { | |
| Write-Host "Successfully set '$RegistryKeyword' for adapter '$AdapterName' to '$NewActualSetValue'." | |
| Write-Host "The change has been applied. An adapter restart may have occurred or might be required for some properties." | |
| } else { | |
| Write-Warning "Failed to set '$RegistryKeyword' to '$TargetValue'. Current value is '$NewActualSetValue'. This might be normal if the property change takes time or requires a restart to reflect." | |
| } | |
| } | |
| else { | |
| Write-Host "The '$RegistryKeyword' property for adapter '$AdapterName' is already set to '$TargetValue'. No action needed." | |
| } | |
| } | |
| catch { # General catch block for errors in Set Mode | |
| Write-Error "An error occurred during Set Mode while managing the network adapter property:" | |
| Write-Error "Adapter : '$AdapterName'" | |
| Write-Error "Registry Keyword : '$RegistryKeyword'" | |
| Write-Error "Target Value : '$TargetValue'" # TargetValue is defined in Set Mode | |
| Write-Error "Error details : $($_.Exception.Message)" | |
| if ($_.FullyQualifiedErrorId) { | |
| Write-Error "Fully Qualified Error ID: $($_.FullyQualifiedErrorId)" | |
| } | |
| Write-Warning "Please ensure the adapter name is correct, the property exists, the target value is supported, and you are running PowerShell as Administrator." | |
| Write-Host "Script execution failed in Set Mode." | |
| } | |
| } | |
| Write-Host "" | |
| Write-Host "Script finished." |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
powershell -executionpolicy bypass -File SetAdapter.ps1 -AdapterName YourAdapterName -RegistryKeyword "*SpeedDuplex" -TargetValue 6