Skip to content

Instantly share code, notes, and snippets.

@fadlee
Last active June 19, 2025 04:42
Show Gist options
  • Save fadlee/8d8323f976553a1a1c79748a0e625492 to your computer and use it in GitHub Desktop.
Save fadlee/8d8323f976553a1a1c79748a0e625492 to your computer and use it in GitHub Desktop.
Connect to wifi via command line
# Enhanced WiFi Connection Script with Auto Location Enable
# Requires Administrator privileges
# Check if running as administrator
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "This script requires Administrator privileges to modify location settings." -ForegroundColor Red
Write-Host "Please restart PowerShell as Administrator and run the command again." -ForegroundColor Yellow
Write-Host "Right-click PowerShell > Run as Administrator, then run your irm command again." -ForegroundColor Cyan
Write-Host "Press any key to exit..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
Write-Host "Running with Administrator privileges..." -ForegroundColor Green
# Function to enable location services
function Enable-LocationServices {
try {
Write-Host "Enabling Location Services..." -ForegroundColor Yellow
# Enable location service
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location" -Name "Value" -Value "Allow" -Force
# Enable location for system
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\lfsvc\Service\Configuration" -Name "Status" -Value 1 -Force
# Start location service if not running
$locationService = Get-Service -Name "lfsvc" -ErrorAction SilentlyContinue
if ($locationService -and $locationService.Status -ne "Running") {
Start-Service -Name "lfsvc" -ErrorAction SilentlyContinue
}
Write-Host "Location Services enabled successfully!" -ForegroundColor Green
return $true
} catch {
Write-Host "Warning: Could not automatically enable location services: $($_.Exception.Message)" -ForegroundColor Yellow
Write-Host "You may need to enable location manually in Settings > Privacy & Security > Location" -ForegroundColor Yellow
return $false
}
}
# Enable location services
Enable-LocationServices
# Wait a moment for services to initialize
Start-Sleep -Seconds 2
# Original WiFi connection code
$ssid = Read-Host "Enter WiFi SSID (Network Name)"
$password = Read-Host "Enter WiFi Password" -AsSecureString
$passwordPlain = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$xmlContent = @"
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>$ssid</name>
<SSIDConfig>
<SSID>
<name>$ssid</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>false</protected>
<keyMaterial>$passwordPlain</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
"@
$tempFile = "$env:TEMP\wifi-temp.xml"
$xmlContent | Out-File -FilePath $tempFile -Encoding UTF8
try {
Write-Host "Adding WiFi profile..." -ForegroundColor Yellow
$result = netsh wlan add profile filename="$tempFile"
Write-Host $result -ForegroundColor Green
# Wait a moment before connecting
Start-Sleep -Seconds 1
Write-Host "Connecting to $ssid..." -ForegroundColor Yellow
$connect = netsh wlan connect ssid="$ssid" name="$ssid"
Write-Host $connect -ForegroundColor Green
# Check connection status
Start-Sleep -Seconds 3
$status = netsh wlan show interfaces | Select-String "State"
if ($status -match "connected") {
Write-Host "Successfully connected to WiFi!" -ForegroundColor Green
} else {
Write-Host "Connection attempt completed. Check your network settings if not connected." -ForegroundColor Yellow
}
} catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
} finally {
if (Test-Path $tempFile) {
Remove-Item $tempFile -Force
Write-Host "Temporary file cleaned up." -ForegroundColor Gray
}
}
Write-Host "Press any key to exit..." -ForegroundColor Cyan
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment