Skip to content

Instantly share code, notes, and snippets.

@realslacker
Last active August 29, 2018 21:50
Show Gist options
  • Save realslacker/6ec32906f3c2e09be65c302c8280216c to your computer and use it in GitHub Desktop.
Save realslacker/6ec32906f3c2e09be65c302c8280216c to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Removes a wireless profile from an exported XML file.
.DESCRIPTION
Removes a wireless profile from an exported XML file, clears the version from the registry.
Author: Shannon Brooks
Copyright: 2018 (c) Shannon Brooks
Version: 2018.8.29.1212
.PARAMETER Path
Path to the wireless profile XML file.
.PARAMETER Name
Name of the wireless profile.
.PARAMETER MaxVersion
The maximum version (non-inclusive) to remove. If ommitted all versions will be removed.
.PARAMETER SettingsPath
The registry location to store the installed version. Defaults to HKLM\Software\WPKG\Settings. Always creates a sub-key 'Wi-Fi'.
.EXAMPLE
Get-Item *.xml | .\Remove-WiFiProfile.ps1 -MaxVersion 2.0
.EXAMPLE
.\Remove-WiFiProfile.ps1 -Path .\Profile.xml
.LINK
https://gist.github.com/realslacker/6ec32906f3c2e09be65c302c8280216c
#>
[CmdletBinding(DefaultParameterSetName='ByFile')]
param(
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName='ByFile')]
[string[]]
$Path,
[Parameter(Mandatory, ParameterSetName='ByName')]
[string[]]
$Name,
[System.Version]
$MaxVersion,
[string]
$SettingsPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WPKG\Settings"
)
begin {
$ErrorActionPreference = 'Continue'
$InformationPreference = 'Continue'
# check settings path
$WiFiSettingsPath = Join-Path $SettingsPath "Wi-Fi Profiles"
if ( -not(Test-Path -Path $WiFiSettingsPath ) ) {
Write-Verbose "Creating registry key '$WiFiSettingsPath'"
New-Item -Path $WiFiSettingsPath -Force > $null
}
# check that WLAN AutoConfig service is running
if ( -not((Get-Service -Name WlanSvc).Status -eq 'Running') ) {
Write-Information "WLAN AutoConfig service is not running, computer may not have wireless adapters, exiting"
$Host.SetShouldExit(0)
exit
}
# get wireless interfaces
$WirelessInterfaces = netsh wlan show interfaces | Select-String -Pattern 'Name\s+:\s+(.*)$' -AllMatches | %{ $_.Matches.Groups[1].Value }
if ( -not( $WirelessInterfaces ) ) {
Write-Information "No wireless interfaces found, exiting"
$Host.SetShouldExit(0)
exit
}
$WirelessInterfaces | ForEach-Object {
Write-Verbose "Found wireless interface '$_'"
}
}
process {
if ( $PsCmdlet.ParameterSetName -eq 'ByName' ) {
$Items = $Name
} else {
$Items = $Path
}
foreach ( $Item in $Items ) {
if ( $PsCmdlet.ParameterSetName -eq 'ByPath' ) {
$PathItem = Get-Item -Path (Resolve-Path -Path $Item)
# check that profile we are trying to import exists
if ( -not( Test-Path -Path $PathItem ) ) {
Write-Error "Could not find wireless profile settings file!"
$Host.SetShouldExit(131)
exit
} else {
Write-Verbose "Using wireless profile settings file '$($PathItem.Name)'"
}
# parse the profile name from the settings file
try {
$ProfileXml = [xml](Get-Content -Path $PathItem -Raw)
$ProfileName = $ProfileXml.WLANProfile.name
Write-Verbose "Found WLAN profile '$ProfileName' in configuration file"
} catch {
Write-Error "Could not parse profile name, possibly bad XML profile."
$Host.SetShouldExit(152)
exit
}
} else {
$ProfileName = $Item
}
# check if the profile is installed
if ( netsh.exe wlan show profiles | Select-String ":\s+${ProfileName}$" ) {
Write-Verbose "Found configuration for profile on computer"
if ( $MaxVersion ) {
Write-Verbose "Only removing profiles prior to v$MaxVersion"
# get the current profile version from the registry
$ProfileVersion = [System.Version](Get-ItemProperty -Path $WiFiSettingsPath -Name $ProfileName -ErrorAction SilentlyContinue).$ProfileName
# check if installed profile is older than the max version
if ( $ProfileVersion -gt $MaxVersion ) {
Write-Warning "Profile '$ProfileName' v$ProfileVersion is newer than v$MaxVersion, skipping"
continue
} else {
Write-Verbose "Found profile version $ProfileVersion"
}
}
# remove the profile
if ( -not( netsh.exe wlan delete profile name="$ProfileName" interface=* | Select-String 'deleted from interface' ) ) {
Write-Error "Profile '$ProfileName' was not deleted!"
$Host.SetShouldExit(1)
exit
}
Write-Information "Profile '$ProfileName' was removed"
# remove the registry setting key
Remove-ItemProperty -Path $WiFiSettingsPath -Name $ProfileName -Force -Confirm:$false -ErrorAction SilentlyContinue
} else {
Write-Warning "Profile '$ProfileName' wasn not found on this computer"
$Host.SetShouldExit(0)
exit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment