Disable IPv6 if enabled, else do nothing

Hi All,

I am relatively new to PowerShell, just trying to practice automation scripts that would be useful our company. In essence, I want to disable IPv6 if it is enabled, else if it is disabled, do nothing. Either way, it should return a result through Write-Host. I’m not sure where I’m going wrong here. I may have over analyzed this :smiley:

$DisableIPv6 = Disable-NetAdapterBinding -InterfaceAlias "*" -ComponentID ms_tcpip6
$tcpip6 = Get-NetAdapterBinding -Name "*" -ComponentID "*"

if ($tcpip6.ComponentID -eq "ms_tcpip6" -and Get-NetAdapterBinding Enabled -eq "True") {

    Write-Host "IPv6 is Enabled"

    }
elseif ($tcpip6 -gt 0) {

    $DisableIPv6
    Write-Host "IPv6 is enabled"
}

else ($tcpip -eq "ms_tcpip") {

Scott,
Welcome to the forum. :wave:t4:

If it is about automation you don’t want to use Write-Host. In fact you actually almost never want to use Write-Host. Here you can read more:

To disable all existing IPv6 bindings on a given computer this should be enough:

Get-NetAdapterBinding -ComponentID 'ms_tcpip6' |
    Where-Object -Property Enabled -EQ -Value $true |
        Disable-NetAdapterBinding

If you want to log the status - maybe before and after your actions you may output the result of the query to a file.

$Path = 'C:\Users\Public\AutomationLogs'
if (-not (Test-Path -Path $Path)) {
    New-Item -Path $Path -ItemType Directory | Out-Null
}

$LogBefore = Join-Path -Path $Path -ChildPath 'IPv6NetAdapterBinding-before.csv'
$logAfter = Join-Path -Path $Path -ChildPath 'IPv6NetAdapterBinding-after.csv'

$IPv6NetAdapterBinding = Get-NetAdapterBinding -ComponentID ms_tcpip6
$IPv6NetAdapterBinding | 
Export-Csv -Path $LogBefore -NoTypeInformation

$IPv6NetAdapterBinding |
    Where-Object -Property Enabled -EQ -Value $true |
        Disable-NetAdapterBinding

$IPv6NetAdapterBinding = Get-NetAdapterBinding -ComponentID ms_tcpip6
$IPv6NetAdapterBinding | 
Export-Csv -Path $logAfter -NoTypeInformation -Force
1 Like

Thanks @Olaf ! I had to modify the script slightly to a directory that already existed: c:\temp, but it worked. Still learning to use Get-Help and all the goodies it has in store.

I did notice that the folder was not created in Public from Export-Csv. When I changed it to an existing directory it worked fine. Any idea why that is?

To me it always feels a kind of unprofessional to save something to a temp directory if it’s not actually temporary.

I updated my code suggestion with the check if the directory exists and if it does not create it. If we do it we try to do it right at the first time. :wink:

And of course you could have just used the folder C:\Users\Public as it always exists on Windows computers. And you could even use the environment variable to specify it $env:PUBLIC.

That worked! I do agree that temp directories are not the best, but I am still learning and this is a personal computer :slight_smile:

Thank you!

Not sure if you’re interested but I could give you a ton of reasons why disabling IPv6 must not be done on Windows systems.

It doesn’t matter if computer is on IPv4 network or not, Windows requires IPv6 enabled.

Sure, you could give me reasons or sources.

I’ve had many instances where IPv6 conflicts with certain VPN and RDP connections, hence why we disable it a lot of the time.

Metablaster, I would be interested in hearing the reasons why disabling IPV6 is a bad idea. I have used NETSH to do this many times with no issues that I am aware of.

Thanks Metablaster.

Most important reason is that MS does OS testing with IPv6 enabled, this means if you disable it there is no quarantee that things will work by design.

The worst thing that may happen is when you need to troubleshoot problems with IPv6 disabled.
Windows is designed with IPv6 in mind, not without.

I don’t know if you ever heard of “nasal demons”, but things can get close to that when troubleshooting networking.

I had a good link about IPv6 but site is no longer online unfortuantelly.

It’s the same with UAC… so many people I know got in the habit of disabling it that it’s hard to get them to consider why they still do. I haven’t seen a legitimate issue caused my UAC in years. Now IPv6 I have seen issues resolved by disabling it recently. There is usually a better, alternate solution.

2 Likes

Thanks for the IPV6 info. This article seemed to sum it up pretty well.

1 Like