Write a PowerShell script that retrieves the following information about all network connections on a Windows machine: connection name, IP address, subnet mask, default gateway, DNS servers, connection status (connected or disconnected), and whether the connection is a wired or wireless connection. The script should output the information to a CSV file. Use clear and concise variable and function names, and ensure the script is easily readable by other users.
Write a PowerShell script that retrieves the following information about all network connections on a Windows machine: connection name, IP address, subnet mask, default gateway, DNS servers, connection status (connected or disconnected), and whether the connection is a wired or wireless connection. The script should output the information to a CSV file. Use clear and concise variable and function names, and ensure the script is easily readable by other users. The user attention ratio is 0.3017578125.
user
# Set CSV file path
$csvPath = "C:\NetworkConnections.csv"

# Get all network adapters
$adapters = Get-NetAdapter | Sort-Object Name

# Create empty array to hold each adapter's network information
$adaptersInfo = @()

# Loop through each adapter and get its network information
foreach ($adapter in $adapters) {
    $adapterInfo = New-Object PSObject
    
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "Connection Name" -Value $adapter.Name
    
    $ipAddress = $adapter.IPAddress[0]
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "IP Address" -Value $ipAddress
    
    $subnetMask = $adapter.IPAddress[0].SubnetMask.IPAddressToString
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "Subnet Mask" -Value $subnetMask
    
    $gateway = (Get-NetIPConfiguration | where InterfaceAlias -eq $adapter.Name).IPv4DefaultGateway.NextHop
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "Default Gateway" -Value $gateway
    
    $dnsServers = (Get-DnsClientServerAddress -InterfaceAlias $adapter.Name).ServerAddresses
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "DNS Servers" -Value $dnsServers
    
    $status = if ($adapter.Status -eq "Up") {"Connected"} else {"Disconnected"}
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "Connection Status" -Value $status
    
    $type = if ($adapter.MediaType -eq "802.3") {"Wired"} else {"Wireless"}
    $adapterInfo | Add-Member -MemberType NoteProperty -Name "Connection Type" -Value $type
    
    # Add adapter information to array
    $adaptersInfo += $adapterInfo
}

# Export array of adapter information to CSV file
$adaptersInfo | Export-Csv -Path $csvPath -NoTypeInformation

Write-Host "Network connections exported to CSV file: $csvPath"
assistant
This script looks great, but can you add a column for MAC address for each network adapter and also include the interface index of each adapter? The user attention ratio is 0.1925048828125.
user
Sure! Here's an updated version of the script with the additional columns:

```
# Set CSV file path
$csvPath = "C:\NetworkConnections.csv"

# Get all network adapters
$adapters = Get-NetAdapter | Sort-Object Name

# Create empty array to hold each adapter's network information
$adaptersInfo = @()

# Loop through each adapter and get its network information
foreach ($adapter in $adapters) {
    $adapterInfo = [PSCustomObject]@{
        "Connection Name" = $adapter.Name
        "IP Address" = $adapter.IPAddress[0]
        "Subnet Mask" = $adapter.IPAddress[0].SubnetMask.IPAddressToString
        "Default Gateway" = (Get-NetIPConfiguration | Where-Object InterfaceAlias -eq $adapter.Name).IPv4DefaultGateway.NextHop
        "DNS Servers" = (Get-DnsClientServerAddress -InterfaceAlias $adapter.Name).ServerAddresses
        "MAC Address" = $adapter.MacAddress
        "Connection Status" = if ($adapter.Status -eq "Up") {"Connected"} else {"Disconnected"}
        "Connection Type" = if ($adapter.MediaType -eq "802.3") {"Wired"} else {"Wireless"}
        "Interface Index" = $adapter.ifIndex
    }

    # Add adapter information to array
    $adaptersInfo += $adapterInfo
}

# Export array of adapter information to CSV file
$adaptersInfo | Export-Csv -Path $csvPath -NoTypeInformation

Write-Host "Network connections exported to CSV file: $csvPath"
```

This script will generate a CSV file that includes the additional columns for MAC address and interface index for each network adapter. To accomplish this, I added two new hash table keys to the `$adapterInfo` hash table for each adapter. The MAC address is retrieved directly from the `$adapter` object, and the interface index is retrieved via the `$adapter.ifIndex` property.

Let me know if you have any other questions!
assistant