Enable and Disable an Network Adapter using PowerShell

u can type and run ” ncpa.cpl” in powershell
it will open windows network adapter listing and setting

1. WMIC
in powershell (Run as Admin):

wmic nic get name","index


PS C:\Users\chako> wmic nic get name","index
Index  Name
0      Intel(R) Ethernet Connection I217-V
1      Realtek PCIe GBE Family Controller
2      Broadcom 802.11ac Network Adapter
3      Microsoft Kernel Debug Network Adapter
4      Microsoft Wi-Fi Direct Virtual Adapter
5      WAN Miniport (SSTP)
6      WAN Miniport (IKEv2)
7      WAN Miniport (L2TP)
8      WAN Miniport (PPTP)
9      WAN Miniport (PPPOE)
10     WAN Miniport (IP)
11     WAN Miniport (IPv6)
12     WAN Miniport (Network Monitor)
13     Microsoft ISATAP Adapter
14     Microsoft Teredo Tunneling Adapter
15     Bluetooth Device (Personal Area Network)
16     Bluetooth Device (RFCOMM Protocol TDI)
17     Microsoft KM-TEST Loopback Adapter
18     VMware Virtual Ethernet Adapter for VMnet1
19     VMware Virtual Ethernet Adapter for VMnet8
25     VirtualBox Host-Only Ethernet Adapter
26     Microsoft ISATAP Adapter #6


PS C:\Windows\system32> wmic path win32_networkadapter where index=18 call enable
Executing (\\DESKTOP-G0J7PAP\root\cimv2:Win32_NetworkAdapter.DeviceID="18")->enable()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ReturnValue = 0;
};



2. netsh
in powershell (Run as Admin):



PS C:\Windows\system32> netsh interface show interface

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Ethernet
Disabled       Disconnected   Dedicated        Npcap Loopback Adapter
Disabled       Disconnected   Dedicated        VirtualBox Host-Only Network
Disabled       Disconnected   Dedicated        VMware Network Adapter VMnet1
Disabled       Disconnected   Dedicated        VMware Network Adapter VMnet8
Enabled        Connected      Dedicated        Wi-Fi
Enabled        Disconnected   Dedicated        Ethernet 2



Disable Ethernet :

netsh interface set interface "Ethernet" disabled



3. Get-NetAdapter
in powershell (Run as Admin):


PS C:\Windows\system32> Get-NetAdapter

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet 2                Realtek PCIe GBE Family Controller           20 Disconnected 70-A0-20-0B-0A-99          0 bps
Wi-Fi                     Broadcom 802.11ac Network Adapter             9 Up           54-CA-6B-A1-A3-23        78 Mbps
VMware Network Adapte...8 VMware Virtual Ethernet Adapter for ...       3 Not Present  00-50-56-C0-00-08          0 bps
VMware Network Adapte...1 VMware Virtual Ethernet Adapter for ...      10 Disabled     00-50-56-C0-00-01       100 Mbps
VirtualBox Host-Only N... VirtualBox Host-Only Ethernet Adapter        23 Not Present  0A-00-27-00-00-1B          0 bps
Npcap Loopback Adapter    Npcap Loopback Adapter                       19 Not Present  A2-00-CC-CC-FF-5A          0 bps
Ethernet                  Intel(R) Ethernet Connection I217-V          16 Disabled     50-D0-AB-CB-FB-99          0 bps



PS C:\Windows\system32> Get-NetAdapter -Name Ethernet | Enable-NetAdapter
PS C:\Windows\system32> Get-NetAdapter -Name Ethernet | Disable-NetAdapter

Confirm
Are you sure you want to perform this action?
Disable-NetAdapter 'Ethernet'
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
PS C:\Windows\system32>


[reference]
https://support.microsoft.com/en-za/help/192806/how-to-run-control-panel-tools-by-typing-a-command

[Project] The Monty Hall Problem

Recently I watched this Youtube video ( The Monty Hall Problem )

“Put simply: If you pick a goat then swap you will always win the car. And you have a 2/3 probability of picking a goat.”

I was so amazed by this logic and static thinking problem.

I use PowerShell to write this The Monty Hall Problem prove program and as yo can see from the program result screenshot,

there is very high chance you get a car when you swap your answer.

just for fun and for practice 🙂

The_Monty_Hall_Problem

PS C:\Users\chako> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  1532

Powershell script:


##################################################################
#
#   The Monty Hall Problem 
#   Youtube : https://www.youtube.com/watch?v=mhlc7peGlGg   
#
#   Raymond Liu 
#   
#
#   2017 / 09 / 13 v1
#   
################# Settings ################################


$getCar = 100
$Goat   = 0



try {
    
	for ($q=1; $q -le 10; $q++) {
	
        for ($i=1; $i -le 10; $i++) {
	
	        # the PowerShell cmdlet generates random numbers you must always set the Maximum value 
		    # to one number beyond your desired limt to fit within the accepted range
	        $TempA      = Get-Random -Maximum 4 -Minimum 1
            $TempB      = Get-Random -Maximum 4 -Minimum 1
		
            if ($TempA  -eq $TempB) {
	            $check = "X"
				$getCar--
	        } else {
	            $check = "O"
	        }
		
		    Write-Host(" " + $check + " ") -ForegroundColor Red -NoNewline
	    }
		Write-Host(" ");
	}
	
    $Goat = 100 - $getCar

    Write-Host(" `n") 
    Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
    Write-Host("Goat: " + $Goat + "%`n`n") -ForegroundColor Yellow
 
} catch {
 
    Write-Host("`n[ERROR] Something Wrong. `n") -ForegroundColor Red
    Exit 1 
 
}

🙂
🙂

Powershell script with recursive:


##################################################################
#
#   The Monty Hall Problem 
#   Youtube : https://www.youtube.com/watch?v=mhlc7peGlGg   
#
#   Raymond Liu 
#   
#
#   2017 / 09 / 14 v2 recursive
#   
################# Settings ################################


$global:getCar = 100
$global:Goat   = 0

$count  = 10

$recursivecount = 1


try {
    
	Function recursive{
	
        $count--
		
		
        If($count -ge 0){
		
		    # the PowerShell cmdlet generates random numbers you must always set the Maximum value 
		    # to one number beyond your desired limt to fit within the accepted range
	        $TempA      = Get-Random -Maximum 4 -Minimum 1
            $TempB      = Get-Random -Maximum 4 -Minimum 1
		    
			if ($TempA  -eq $TempB) {
	            $check = "X"
				$global:getCar --
				#Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
	        } else {
	            $check = "O"
	        }
			
            Write-Host(" " + $check + " ") -ForegroundColor Red -NoNewline
			
			if ($count -eq 0 -and  $recursivecount -lt 10) {
			    $count = 10
				$recursivecount++
				Write-Host(" ");
			}
            recursive
        }
    }

    recursive
	$Goat = 100 - $getCar

                Write-Host(" `n") 
                Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
                Write-Host("Goat: " + $Goat + "%`n`n") -ForegroundColor Yellow
 
} catch {
 
    Write-Host("`n[ERROR] Something Wrong. `n") -ForegroundColor Red
    Exit 1 
 
}


Create project environment for class 20-410

This script is used in class Microsoft(20-410) for setting up project environment.


##################################################################
#   * Please create hyper-V network switch manually
#   * Set switch to be private. Name "Private Network"
#   * Please run this script as Administrator privilege
#   
#   Raymond Liu 
#   Team: Round 3
#
#   2016 / 10 / 26 v1
#   2016 / 11 / 9  v1.3
################# Settings ################################

$MECO_DC1      = "MECO-DC1"                                                                 # Implement server with: AD DS, DNS, DHCP
$MECO_DC2      = "MECO-DC2"                                                                 # Implement Secondary AD DS role only
$MECO_FS1      = "MECO-FS1"                                                                 # Implement domain joined File Server
$MECO_WS1      = "MECO-WS1"                                                                 # Implement domain joined Intranet Web Server
$MECO_CL1      = "MECO-CL1"                                                                 # Implement Domain joined client workstation
$MECO_CL2      = "MECO-CL2"                                                                 # Implement Domain joined client workstation
$MECO_RTR1     = "MECO-RTR1"                                                                # Implement Domain joined RRAS Routing Server and VPN
$MECO_NAT      = "MECO-NAT"                                                                 # Implement NAT Serevr
$RAM           = 2GB  				                                                        # RAM assigned to VM
$VHD           = 127GB  			                                                        # Size of Hard-Drive for VM
$GENERATION    = 1                                                                          # VM Generation 
$VM_Folder     = "C:\Project"                                                               # Location of the VM and VHDX files
$VMSwitch      = "Private Network"                                                          # Name of the Hyper-v Network Switch
$VMSwitch2     = "External"                                                                 # Name of the Hyper-v Network Switch
$W8ISO         = "..\OS\Windows 8.1 Enterprise 64BIT_English_MLF_X18-96759.iso"	            # Windows 8.1 ISO
$W12ISO        = "..\OS\en_windows_server_2012_r2_with_update_x64_dvd_6052708.iso"	        # Windows Server 2012 R2 ISO

################# Settings ################################

################# Create Hyper-V VM folder ################
# Create Hyper-V VM folder
try {

    MD $VM_Folder -ErrorAction SilentlyContinue

} catch {

    Write-Host("`n[ERROR] Can't Create VM Folder. `n") -ForegroundColor Red
    Exit 1 

}

################# Create Hyper-V VM folder ################

################# Create VM ###############################
# Create VM
try {

    New-VM -Name $MECO_DC1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_DC1"\Virtual Hard Disks\"$MECO_DC1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_DC1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_DC1

try {

    New-VM -Name $MECO_DC2 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_DC2"\Virtual Hard Disks\"$MECO_DC2.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_DC2. `n") -ForegroundColor Red
    Exit 1 

} # MECO_DC2

try {

    New-VM -Name $MECO_FS1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_FS1"\Virtual Hard Disks\"$MECO_FS1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_FS1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_FS1


try {

    New-VM -Name $MECO_WS1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_WS1"\Virtual Hard Disks\"$MECO_WS1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_WS1


try {

    New-VM -Name $MECO_CL1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_CL1"\Virtual Hard Disks\"$MECO_CL1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_CL1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_CL1


try {

    New-VM -Name $MECO_CL2 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_CL2"\Virtual Hard Disks\"$MECO_CL2.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_CL2. `n") -ForegroundColor Red
    Exit 1 

} # MECO_CL2


try {

    New-VM -Name $MECO_RTR1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_RTR1"\Virtual Hard Disks\"$MECO_RTR1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_RTR1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_RTR1


try {

    New-VM -Name $MECO_NAT `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_NAT"\Virtual Hard Disks\"$MECO_NAT.vhdx `
           -NewVHDSizeBytes $VHD `
		   -SwitchName $VMSwitch2 `
           -Generation $GENERATION
		   
    Add-VMNetworkAdapter -VMName $MECO_NAT -SwitchName $VMSwitch

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_NAT. `n") -ForegroundColor Red
    Exit 1 

} # MECO_NAT

################# Create VM ###############################


################# Set VM ISO ##############################
# Set VM ISO
try {

    Set-VMDvdDrive -VMName $MECO_DC1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_DC1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_DC1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_DC1

try {

    Set-VMDvdDrive -VMName $MECO_DC2 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_DC2) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_DC2. `n") -ForegroundColor Red
    Exit 1 
} # MECO_DC2

try {

    Set-VMDvdDrive -VMName $MECO_FS1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_FS1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_FS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_FS1

try {

    Set-VMDvdDrive -VMName $MECO_WS1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_WS1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_WS1

try {

    Set-VMDvdDrive -VMName $MECO_CL1 `
                   -Path $W8ISO


    Write-Host("Done Create VM: --> " + $MECO_CL1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_CL1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_CL1


try {

    Set-VMDvdDrive -VMName $MECO_CL2 `
                   -Path $W8ISO


    Write-Host("Done Create VM: --> " + $MECO_CL2) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_CL2. `n") -ForegroundColor Red
    Exit 1 
} # MECO_CL2


try {

    Set-VMDvdDrive -VMName $MECO_RTR1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_RTR1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_RTR1


try {

    Set-VMDvdDrive -VMName $MECO_NAT `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_NAT + "`n") -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_NAT. `n") -ForegroundColor Red
    Exit 1 
} # MECO_NAT



################# Set VM ISO ##############################


# Star VM
try {

    #Start-VM $MECO_DC1

} catch {

    #Write-Host("`n[ERROR] Can't Start VM MECO_DC1. `n") -ForegroundColor Red
    #Exit 1 

}



Exit 1