Results 1 to 9 of 9

Thread: Powershell Script to Enable/Disable Network Connection

  1. #1
    Join Date
    Nov 2008
    Posts
    1,185

    Powershell Script to Enable/Disable Network Connection

    Can I write a Powershell Script that will toggle a Network Connection in Windows Vista? By toggle I mean Enable or Disable. Can I also toggle the Firewall on a Network Connection? Is there any one who can provide a code or point to a guide or tutorial achieving this?

  2. #2
    Join Date
    May 2008
    Posts
    3,971

    Re: Powershell Script to Enable/Disable Network Connection

    Here is a PowerShell script that will scan your adapters for VMWare’s virtual NICs and make the necessary registry changes. It will also enable/disable cycle the adapters so that the changes take effect.

    Code:
    # *NdisDeviceType   
    # The type of the device. The default value is zero, which indicates a standard  networking device that connects to a network.  
    # Set *NdisDeviceType to NDIS_DEVICE_TYPE_ENDPOINT (1) if this device is an endpoint device and is not a true network interface that connects to a network.  
    # For example, you must specify NDIS_DEVICE_TYPE_ENDPOINT for devices such as  
    # smart phones that use a networking infrastructure to communicate to the local  
    # computer system but do not provide connectivity to an external network.   
    
    # Usage: run in an elevated shell (vista/longhorn) or as adminstrator (xp/2003).  
    
    # PS> .\fix-vmnet-adapters.ps1  
      
    # boilerplate elevation check  
      
     $identity = [Security.Principal.WindowsIdentity]::GetCurrent()  
     $principal = new-object Security.Principal.WindowsPrincipal $identity 
     $elevated = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)  
      
     if (-not $elevated) {  
         $error = "Sorry, you need to run this script" 
         if ([System.Environment]::OSVersion.Version.Major -gt 5) {  
             $error += " in an elevated shell." 
         } else {  
             $error += " as Administrator." 
         }  
         throw $error 
     }  
      
     function confirm {  
         $host.ui.PromptForChoice("Continue", "Process adapter?",  
             [Management.Automation.Host.ChoiceDescription[]]@("&No", "&Yes"), 0) -eq $true 
     }  
      
     # adapters key  
     pushd 'hklm:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}' 
      
     # ignore and continue on error  
     dir -ea 0  | % {  
         $node = $_.pspath  
         $desc = gp $node -name driverdesc  
         if ($desc -like "*vmware*") {  
             write-host ("Found adapter: {0} " -f $desc.driverdesc)  
             if (confirm) {  
                 new-itemproperty $node -name '*NdisDeviceType' -propertytype dword -value 1  
             }  
         }  
     }  
     popd  
      
     # disable/enable network adapters  
     gwmi win32_networkadapter | ? {$_.name -like "*vmware*" } | % {  
           
         # disable  
         write-host -nonew "Disabling $($_.name) ... " 
         $result = $_.Disable()  
         if ($result.ReturnValue -eq -0) { write-host " success." } else { write-host " failed." }  
           
         # enable  
         write-host -nonew "Enabling $($_.name) ... " 
         $result = $_.Enable()  
         if ($result.ReturnValue -eq -0) { write-host " success." } else { write-host " failed." }  
     }

  3. #3
    Join Date
    May 2008
    Posts
    1,196

    Re: Powershell Script to Enable/Disable Network Connection

    First get your adapter information.

    PS C:\Users\James> get-wmiobject win32_networkadapter | format-table ServiceName MACAddress AdapterType DeviceID Name NetworkAdd Speed

    Verify you have selected the correct device ID
    Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 4}

    ServiceName : RTL8023xp
    MACAddress : xx:xx:xx:xx:xx:xx
    AdapterType : Ethernet 802.3
    DeviceID : 4
    Name : Realtek RTL8139/810x Family Fast Ethernet NIC
    NetworkAddresses :
    Speed : 100000000


    next set a PS variable in my case I called it $realtek
    PS C:\Users\James> $realtek = Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 4}

    remember that variables are only active for your current PS session so
    you cannot close PS and expect these commands to work.

    since I used $realtek as my variable I can use these commands to turn on and off My network adapter
    $realtek.disable()
    $realtek.enable()

    If you want to create a script you have to allow for the execution of them.
    PS C:\Users\James> Get-ExecutionPolicy

    if they are not allowed you'll have to run this command

    PS C:\Users\James> Set-ExecutionPolicy RemoteSigned
    this will allow local scripts to run but should ask permission before running downloaded scripts. You can set it for unrestricted but I would not recommend it.

    IF you are running Firefox, check before you change this setting, I am not absolutely sure but I think Firefox is different and it does not set the alternate data stream to indicate a script was downloaded from the internet. This may allow them to run.

    Now you would need to create a Powershell profile

    To create a profile visit http://msdn.microsoft.com/en-us/libr...88(VS.85).aspx

    Now once you have your profile created open it with notepad

    PS C:\Users\James> notepad $profile

    add your variable to your profile

    $realtek = Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 4}

    this will make the variable available everytime you launch PS

    Create a shortcuts so that all you need to do is click on enable or disable to turn the network connection on and off.

    Create a shortcut and call it "Disable net" in the target option place
    this line

    C:\Windows\System32\WindowsPowerShell\v1.0\powersh ell.exe $realtek.disable()

    change the variable to the one you created

    Create another and call it "Enable Net". The target would be
    C:\Windows\System32\WindowsPowerShell\v1.0\powersh ell.exe $realtek.enable()

  4. #4
    Join Date
    May 2008
    Posts
    816

    Re: Powershell Script to Enable/Disable Network Connection

    To enable,

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()

    And to disable.

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "Disa&ble"}).DoIt()

  5. #5
    Join Date
    May 2008
    Posts
    2,945

    Re: Powershell Script to Enable/Disable Network Connection

    To manage the Windows Firewall with Powershell, use the LocalPolicy.CurrentProfile. FirewallEnabled property of the HNetCfg.FwMgr COM object:

    PS >$firewall = New-Object -com HNetCfg.FwMgr
    PS >$firewall.LocalPolicy.CurrentProfile.FirewallEnabled = $true
    PS >$firewall.LocalPolicy.CurrentProfile.FirewallEnabled
    True

  6. #6
    Join Date
    Nov 2009
    Posts
    1

    Re: Powershell Script to Enable/Disable Network Connection

    Quote Originally Posted by Baltazar View Post
    To enable,

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()

    And to disable.

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "Disa&ble"}).DoIt()
    Hi Baltazar,

    I have tried to use your scripts to enable and disable Wireless Network Connection, it work very well. But I have another issue is to I would like to run your scripts from a shortcut so I have tried to create the shortcut by following the steps from

    http://www.personalmicrocosms.com/pages/blog.aspx?b=17

    and I have replaced the content of the file "Example.ps1" with your scripts.

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Wireless Network Connection"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()

    After I have created the shortcut, I have tried to enable the Wireless Network Connection from the shortcut. It does not work, it show only blank screen of the powershell (with no prompt).

    Please help me for this issue.

    Best Regards,
    Chumpol W.

  7. #7
    Join Date
    Dec 2009
    Posts
    1

    Re: Powershell Script to Enable/Disable Network Connection

    Quote Originally Posted by Baltazar View Post
    To enable,

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()

    And to disable.

    $shell = New-Object -comObject Shell.Application
    ((($shell.NameSpace(3).Items() | where {$_.Name -eq "Network Connections"}).GetFolder.Items() | where {$_.Name -eq "Local Area Connection"}).Verbs() | where {$_.Name -eq "Disa&ble"}).DoIt()


    Hi Baltazar,

    I tried your code to enable/disable network connections. for me it did not work in Windows Vista and 2k8. I tried to modify your code to work on all OS as below.

    To enable


    $shell = New-Object -comObject Shell.Application((($shell.NameSpace(0x31) | where {$_.Title -eq "Network Connections"}).Items() | where {$_.Name -like "Local*"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()



    To Disable
    $shell = New-Object -comObject Shell.Application((($shell.NameSpace(0x31) | where {$_.Title -eq "Network Connections"}).Items() | where {$_.Name -like "Local*"}).Verbs() | where {$_.Name -eq "Disa&ble"}).DoIt()

  8. #8
    Join Date
    Jan 2011
    Posts
    1

    Re: Powershell Script to Enable/Disable Network Connection

    Quote Originally Posted by ravisankar View Post
    Hi Baltazar,

    I tried your code to enable/disable network connections. for me it did not work in Windows Vista and 2k8. I tried to modify your code to work on all OS as below.

    To enable


    $shell = New-Object -comObject Shell.Application((($shell.NameSpace(0x31) | where {$_.Title -eq "Network Connections"}).Items() | where {$_.Name -like "Local*"}).Verbs() | where {$_.Name -eq "En&able"}).DoIt()



    To Disable
    $shell = New-Object -comObject Shell.Application((($shell.NameSpace(0x31) | where {$_.Title -eq "Network Connections"}).Items() | where {$_.Name -like "Local*"}).Verbs() | where {$_.Name -eq "Disa&ble"}).DoIt()
    I tried the above code but it didn't work. $shell.namespace(0x31) allready points you to the "Network Connections" folder so that part can be skipped. I use a dutch windows 7 and the code below worked for me:

    To enable:
    $shell = New-Object -comObject Shell.Application
    (($shell.NameSpace(0x31).Items() | where {$_.Name -like "LAN*"}).Verbs() | where {$_.Name -eq "&Inschakelen"}).DoIt()

    To Disable:
    $shell = New-Object -comObject Shell.Application
    (($shell.NameSpace(0x31).Items() | where {$_.Name -like "LAN*"}).Verbs() | where {$_.Name -eq "&Uitschakelen"}).DoIt()

    Kind regards,

    JP

  9. #9
    Join Date
    Apr 2012
    Posts
    1

    Re: Powershell Script to Enable/Disable Network Connection

    PowerShell 3.0 has a new cmdlet called Enable-NetworkAdapter

Similar Threads

  1. How enable network adapter in script?
    By maketu in forum XP Hardware
    Replies: 3
    Last Post: 06-05-2012, 07:35 PM
  2. Batch file to disable or enable network adapter
    By ToniYap in forum Networking & Security
    Replies: 2
    Last Post: 25-06-2010, 12:38 AM
  3. Renaming network connection script
    By deepakbabbar in forum Operating Systems
    Replies: 1
    Last Post: 02-07-2009, 10:56 PM
  4. How to enable ICS on a network connection
    By Aanand in forum Technology & Internet
    Replies: 3
    Last Post: 29-04-2009, 02:19 PM
  5. Enable/Disable Network connection at once
    By Fabio Rosa in forum Windows Server Help
    Replies: 6
    Last Post: 17-02-2009, 07:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,672,687.25238 seconds with 17 queries