Skip to content

Instantly share code, notes, and snippets.

@AlexLaforge
Forked from simply-coded/Disable_Wi-Fi.vbs
Created December 8, 2021 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexLaforge/5e9aed2e85dd179eec3ad7fb5b4685ea to your computer and use it in GitHub Desktop.
Save AlexLaforge/5e9aed2e85dd179eec3ad7fb5b4685ea to your computer and use it in GitHub Desktop.
Use VBScript to enable, disable, or toggle a connection like your Wi-Fi on and off.
'************************
'Name: Disable Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'************************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to disable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
Dim success : success = False
For Each verb In interfaceTarget.Verbs
If verb.Name = "Disa&ble" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Disabled: """ & interfaceName & """", vbInformation
success = True
Exit For
End If
Next
If Not success Then
MsgBox "Already disabled : """ & interfaceName & """", vbInformation
End If
'***********************
'Name: Enable Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'***********************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to enable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
Dim success : success = False
For Each verb In interfaceTarget.Verbs
If verb.Name = "En&able" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Enabled: """ & interfaceName & """", vbInformation
success = True
Exit For
End If
Next
If Not success Then
MsgBox "Already enabled : """ & interfaceName & """", vbInformation
End If
'***********************
'Name: Toggle Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'***********************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to disable/enable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
For Each verb In interfaceTarget.Verbs
If verb.Name = "En&able" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Enabled: """ & interfaceName & """", vbInformation
ElseIf verb.Name = "Disa&ble" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Disabled: """ & interfaceName & """", vbInformation
End If
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment