Skip to main content

Clearing Wi-DAS from managed machines

Uninstalling your app leaves the Passpoint profile, the client certificate and the device key behind. An MSIX package has no uninstall hook to remove them, and an MDM's uninstall only removes the package (see When your app is uninstalled). If you removed the app without calling Disconnect() first, this script clears up what's left. Your MDM can run it across every machine.

It removes what the Wi-DAS SDK installed for your app and nothing else. Other Wi-Fi profiles, other apps' certificates (including other Wi-DAS apps') and anything machine-wide are left alone.

Clearing a machine is tidying. It doesn't cut off access, and it can't take back a certificate that's been copied elsewhere. To take a user or machine off the network, ask Wi-DAS to revoke the certificate.

What it removes​

The script works on the signed-in user, because that's where the SDK installs everything. It looks for that user's Wi-DAS certificates for the Client IDs you give it, then removes:

WhatWhich onesWhere
Client certificatesThose issued for your Client ID that use this user's Wi-DAS device keyThe user's personal store (CurrentUser\My)
Passpoint profilesPer-user profiles for those certificates' realms, on every wireless interfaceWindows' Wi-Fi profiles
Issuing CAThe copy the SDK added, once no certificate left in the personal store was issued by itThe user's intermediate store (CurrentUser\CA)
Device keyOnly with -IncludeDeviceKey, and only once no certificate left uses itThe user's key storage

It never removes:

  • All-user or Group Policy Wi-Fi profiles, including a Passpoint profile your MDM pushed for every user. Remove those through your MDM (see Configuring Passpoint with Intune or another MDM). A profile your MDM pushed for one user is the same kind the SDK writes, though, so if it's for the same realm, the script removes it too.
  • Certificates for other Client IDs. Name each of yours if you ship more than one Wi-DAS app.
  • The device key, unless you ask. Every Wi-DAS app on the machine shares one key per user. Keep it if another Wi-DAS app might be installed; deleting it makes that app enrol the machine as a new one.
  • Anything in a machine-wide or root store. The SDK never writes to those.

It finds the certificates through the device key. If a user's key has already gone, the script has nothing to go on and leaves everything as it is.

The script​

Save it as Remove-WiDasArtefacts.ps1, and set your Client ID in the param block if your tool can't pass parameters.

<#
.SYNOPSIS
Removes what the Wi-DAS Windows SDK installed for the signed-in user, and nothing else.

.DESCRIPTION
Finds this user's Wi-DAS client certificates for the Client IDs you name, then removes:
- the per-user Passpoint profiles for those certificates' realms, on every wireless interface
- the certificates themselves, from the user's personal store
- the issuing CA the SDK added to the user's intermediate store, once nothing left needs it
- with -IncludeDeviceKey, the Wi-DAS device key, once no certificate on the machine uses it

All-user and Group Policy profiles, other apps' certificates and every machine-wide store are left
alone. Run it as the user whose Wi-DAS artefacts you're removing, not as SYSTEM.

.PARAMETER ClientId
Your Wi-DAS Client ID, or several if you ship more than one Wi-DAS app. If your tool can't pass
parameters, set the default below instead.

.PARAMETER IncludeDeviceKey
Also delete the Wi-DAS device key. Every Wi-DAS app on the machine shares it, so the script only
deletes it when no certificate left in the user's personal store uses it.

.EXAMPLE
.\Remove-WiDasArtefacts.ps1 -ClientId client-acme -WhatIf

.EXAMPLE
.\Remove-WiDasArtefacts.ps1 -ClientId client-acme -IncludeDeviceKey
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[string[]] $ClientId = @('<your-client-id>'),

[switch] $IncludeDeviceKey = $false
)

Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'

$ClientId = @($ClientId | Where-Object { $_ })
if ($ClientId.Count -eq 0 -or $ClientId -contains '<your-client-id>') {
Write-Error 'Set -ClientId (or its default at the top of this script) to your Wi-DAS Client ID.' -ErrorAction Continue
exit 2
}

# The key, certificate and Wi-Fi checks all need .NET and Win32 calls that Constrained Language Mode
# blocks. Without them nothing can be identified, so stop rather than report an all-clear.
if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') {
Write-Error "PowerShell is running in $($ExecutionContext.SessionState.LanguageMode) mode, so this script can't check anything. Nothing was removed." -ErrorAction Continue
exit 1
}

$DeviceKeyName = 'WiDas.Device'
$KeyProviders = 'Microsoft Platform Crypto Provider', 'Microsoft Software Key Storage Provider'
$IssuingCaMarker = 'Wi-DAS issuing CA, added by the Wi-DAS SDK'
$ProfileNamespace = 'http://www.microsoft.com/networking/WLAN/profile/v1'

$script:failures = 0

function Write-Failure([string] $message) {
$script:failures++
Write-Warning $message
}

# --- The device key -------------------------------------------------------------------------------

function Get-DeviceKey {
foreach ($providerName in $KeyProviders) {
$provider = New-Object Security.Cryptography.CngProvider $providerName
try {
$exists = [Security.Cryptography.CngKey]::Exists($DeviceKeyName, $provider)
}
catch {
# No TPM, or the provider isn't there: there's no key under it.
continue
}
if ($exists) {
# A key that's there but won't open is a failure, not an absence.
[Security.Cryptography.CngKey]::Open($DeviceKeyName, $provider)
}
}
}

function Get-PublicKeyBytes($algorithm) {
# The public half as one byte string, comparable between a CNG key and a certificate.
if ($algorithm -is [Security.Cryptography.ECDsa]) {
$p = $algorithm.ExportParameters($false)
return , [byte[]]($p.Q.X + $p.Q.Y)
}
if ($algorithm -is [Security.Cryptography.RSA]) {
$p = $algorithm.ExportParameters($false)
return , [byte[]]($p.Modulus + $p.Exponent)
}
return $null
}

function Get-KeyPublicBytes([Security.Cryptography.CngKey] $key) {
if ($key.AlgorithmGroup -eq [Security.Cryptography.CngAlgorithmGroup]::ECDsa) {
return , (Get-PublicKeyBytes (New-Object Security.Cryptography.ECDsaCng $key))
}
if ($key.AlgorithmGroup -eq [Security.Cryptography.CngAlgorithmGroup]::Rsa) {
return , (Get-PublicKeyBytes (New-Object Security.Cryptography.RSACng $key))
}
return $null
}

function Get-CertificatePublicBytes([Security.Cryptography.X509Certificates.X509Certificate2] $certificate) {
try {
$ec = [Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::GetECDsaPublicKey($certificate)
if ($ec) { return , (Get-PublicKeyBytes $ec) }
$rsa = [Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($certificate)
if ($rsa) { return , (Get-PublicKeyBytes $rsa) }
}
catch {
# Unreadable: treated as not ours.
}
return $null
}

function Test-BytesEqual([byte[]] $a, [byte[]] $b) {
if ($null -eq $a -or $null -eq $b -or $a.Length -ne $b.Length) { return $false }
for ($i = 0; $i -lt $a.Length; $i++) { if ($a[$i] -ne $b[$i]) { return $false } }
return $true
}

function Test-CertifiesAnyKey($certificate, $keyBytes) {
$certificateBytes = Get-CertificatePublicBytes $certificate
foreach ($bytes in $keyBytes) {
if (Test-BytesEqual $certificateBytes $bytes) { return $true }
}
return $false
}

# --- Certificates ---------------------------------------------------------------------------------

function Read-DerElement([byte[]] $der, [int] $at) {
# One DER element at $at: its tag, where its content starts, and its content length.
if ($at + 2 -gt $der.Length) { throw 'Truncated' }
$tag = $der[$at]
$length = [int] $der[$at + 1]
$start = $at + 2
if ($length -band 0x80) {
$count = $length -band 0x7F
if ($count -lt 1 -or $count -gt 3 -or $start + $count -gt $der.Length) { throw 'Bad length' }
$length = 0
for ($i = 0; $i -lt $count; $i++) { $length = ($length -shl 8) -bor $der[$start + $i] }
$start += $count
}
if ($start + $length -gt $der.Length) { throw 'Truncated' }
[pscustomobject]@{ Tag = $tag; Start = $start; Length = $length; End = $start + $length }
}

function ConvertFrom-DerString([byte[]] $der, $element) {
$bytes = New-Object byte[] $element.Length
[Array]::Copy($der, $element.Start, $bytes, 0, $element.Length)
switch ($element.Tag) {
0x0C { return [Text.Encoding]::UTF8.GetString($bytes) } # UTF8String
0x13 { return [Text.Encoding]::ASCII.GetString($bytes) } # PrintableString
0x16 { return [Text.Encoding]::ASCII.GetString($bytes) } # IA5String
0x14 { return [Text.Encoding]::GetEncoding(28591).GetString($bytes) } # TeletexString, as Latin-1
0x1E { return [Text.Encoding]::BigEndianUnicode.GetString($bytes) } # BMPString
0x1C { return (New-Object Text.UTF32Encoding $true, $false).GetString($bytes) } # UniversalString
}
throw 'Unreadable string'
}

function Get-OrganisationalUnits($certificate) {
# Read from the subject's DER, so a value can't pass itself off as another attribute.
$der = $certificate.SubjectName.RawData
try {
$name = Read-DerElement $der 0
if ($name.Tag -ne 0x30) { return }
$units = @()
$at = $name.Start
while ($at -lt $name.End) {
$set = Read-DerElement $der $at
$inner = $set.Start
while ($inner -lt $set.End) {
$pair = Read-DerElement $der $inner
$oid = Read-DerElement $der $pair.Start
$value = Read-DerElement $der $oid.End
# 2.5.4.11, organizationalUnitName
if ($oid.Tag -eq 0x06 -and $oid.Length -eq 3 -and
$der[$oid.Start] -eq 0x55 -and $der[$oid.Start + 1] -eq 0x04 -and $der[$oid.Start + 2] -eq 0x0B) {
$units += ConvertFrom-DerString $der $value
}
$inner = $pair.End
}
$at = $set.End
}
$units
}
catch {
# A subject that can't be read is never treated as ours.
}
}

function Test-IssuedFor($certificate, [string[]] $clientIds) {
foreach ($ou in (Get-OrganisationalUnits $certificate)) {
foreach ($id in $clientIds) {
if ([string]::Equals($ou, $id, [StringComparison]::Ordinal)) { return $true }
}
}
return $false
}

function Get-Realm($certificate) {
# The subject's common name is user@realm.
$name = $certificate.GetNameInfo([Security.Cryptography.X509Certificates.X509NameType]::SimpleName, $false)
$at = $name.LastIndexOf('@')
if ($at -lt 0) { return $null }
$realm = $name.Substring($at + 1)
if ($realm.Length -eq 0 -or $realm.Length -gt 255) { return $null }
foreach ($label in $realm.Split('.')) {
if ($label -cnotmatch '\A[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\z') { return $null }
}
return $realm
}

# --- Wi-Fi profiles -------------------------------------------------------------------------------

$wlanSource = @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static class WiDasWlan
{
public sealed class Profile
{
public Guid Interface;
public string Name;
public uint Flags;
public string Xml;
public uint ReadError;
}

[DllImport("wlanapi.dll")]
private static extern uint WlanOpenHandle(uint clientVersion, IntPtr reserved, out uint negotiatedVersion, out IntPtr client);

[DllImport("wlanapi.dll")]
private static extern uint WlanCloseHandle(IntPtr client, IntPtr reserved);

[DllImport("wlanapi.dll")]
private static extern uint WlanEnumInterfaces(IntPtr client, IntPtr reserved, out IntPtr list);

[DllImport("wlanapi.dll")]
private static extern uint WlanGetProfileList(IntPtr client, ref Guid radio, IntPtr reserved, out IntPtr list);

[DllImport("wlanapi.dll", CharSet = CharSet.Unicode)]
private static extern uint WlanGetProfile(IntPtr client, ref Guid radio, string name, IntPtr reserved, out IntPtr xml, IntPtr flags, IntPtr access);

[DllImport("wlanapi.dll", CharSet = CharSet.Unicode)]
private static extern uint WlanDeleteProfile(IntPtr client, ref Guid radio, string name, IntPtr reserved);

[DllImport("wlanapi.dll")]
private static extern void WlanFreeMemory(IntPtr memory);

private const int InterfaceInfoSize = 532; // GUID, WCHAR[256], WLAN_INTERFACE_STATE
private const int ProfileInfoSize = 516; // WCHAR[256], DWORD

public static List<Profile> Read(out uint error)
{
var profiles = new List<Profile>();
IntPtr client;
uint negotiated;
error = WlanOpenHandle(2, IntPtr.Zero, out negotiated, out client);
if (error != 0) return profiles;

try
{
IntPtr interfaces;
error = WlanEnumInterfaces(client, IntPtr.Zero, out interfaces);
if (error != 0) return profiles;

try
{
int count = Marshal.ReadInt32(interfaces, 0);
for (int i = 0; i < count; i++)
{
var radio = (Guid)Marshal.PtrToStructure(new IntPtr(interfaces.ToInt64() + 8 + i * InterfaceInfoSize), typeof(Guid));
IntPtr list;
uint listError = WlanGetProfileList(client, ref radio, IntPtr.Zero, out list);
if (listError != 0) { error = listError; continue; }

try
{
int profileCount = Marshal.ReadInt32(list, 0);
for (int j = 0; j < profileCount; j++)
{
var entry = new IntPtr(list.ToInt64() + 8 + j * ProfileInfoSize);
var profile = new Profile
{
Interface = radio,
Name = Marshal.PtrToStringUni(entry),
Flags = (uint)Marshal.ReadInt32(entry, 512)
};

IntPtr xml;
profile.ReadError = WlanGetProfile(client, ref radio, profile.Name, IntPtr.Zero, out xml, IntPtr.Zero, IntPtr.Zero);
if (profile.ReadError == 0)
{
profile.Xml = Marshal.PtrToStringUni(xml);
WlanFreeMemory(xml);
}
profiles.Add(profile);
}
}
finally { WlanFreeMemory(list); }
}
}
finally { WlanFreeMemory(interfaces); }
}
finally { WlanCloseHandle(client, IntPtr.Zero); }

return profiles;
}

public static uint Delete(Guid radio, string name)
{
IntPtr client;
uint negotiated;
uint error = WlanOpenHandle(2, IntPtr.Zero, out negotiated, out client);
if (error != 0) return error;
try { return WlanDeleteProfile(client, ref radio, name, IntPtr.Zero); }
finally { WlanCloseHandle(client, IntPtr.Zero); }
}
}
'@

$PerUserProfile = 0x2
$GroupPolicyProfile = 0x1

function Test-DeclaresRealm([string] $xml, [string[]] $realms) {
try {
$document = New-Object Xml.XmlDocument
$document.LoadXml($xml.TrimStart([char]0xFEFF, [char]0x200B, ' ', "`t", "`r", "`n"))
}
catch {
return $false
}
foreach ($hotspot in $document.DocumentElement.ChildNodes) {
if ($hotspot.LocalName -ne 'Hotspot2' -or $hotspot.NamespaceURI -ne $ProfileNamespace) { continue }
foreach ($nai in $hotspot.ChildNodes) {
if ($nai.LocalName -ne 'NAIRealm' -or $nai.NamespaceURI -ne $ProfileNamespace) { continue }
foreach ($name in $nai.ChildNodes) {
if ($name.LocalName -ne 'name' -or $name.NamespaceURI -ne $ProfileNamespace) { continue }
$value = $name.InnerText.Trim().Trim([char]0xFEFF, [char]0x200B)
foreach ($realm in $realms) {
if ([string]::Equals($value, $realm, [StringComparison]::OrdinalIgnoreCase)) { return $true }
}
}
}
}
return $false
}

function Remove-PasspointProfiles([string[]] $realms) {
if (-not (Test-Path (Join-Path ([Environment]::SystemDirectory) 'wlanapi.dll'))) {
Write-Output 'No Wi-Fi support on this machine, so there are no profiles to remove.'
return
}
try {
Add-Type -TypeDefinition $wlanSource -ErrorAction Stop
}
catch {
if (-not ('WiDasWlan' -as [type])) {
Write-Failure "Couldn't load the Wi-Fi API, so no profiles were checked: $($_.Exception.Message)"
return
}
}

[uint32] $listError = 0
$profiles = [WiDasWlan]::Read([ref] $listError)
if ($listError -ne 0) {
Write-Failure "Windows couldn't list every Wi-Fi profile (error $listError). Is the WLAN AutoConfig service running?"
}

foreach ($profile in $profiles) {
$perUser = ($profile.Flags -band $PerUserProfile) -ne 0 -and ($profile.Flags -band $GroupPolicyProfile) -eq 0
if (-not $perUser) { continue }
if ($profile.ReadError -eq 1168) { continue } # Gone since it was listed.
if ($profile.ReadError -ne 0) {
# Not named: it may be any network the user has joined.
Write-Failure "Couldn't read one of this user's Wi-Fi profiles (error $($profile.ReadError)), so it was left alone."
continue
}
if (-not (Test-DeclaresRealm $profile.Xml $realms)) { continue }

if ($PSCmdlet.ShouldProcess("Wi-Fi profile '$($profile.Name)' on interface $($profile.Interface)", 'Remove')) {
$result = [WiDasWlan]::Delete($profile.Interface, $profile.Name)
if ($result -eq 0) {
Write-Output "Removed the Wi-Fi profile '$($profile.Name)' from interface $($profile.Interface)."
}
elseif ($result -ne 1168) {
Write-Failure "Couldn't remove the Wi-Fi profile '$($profile.Name)' (error $result)."
}
}
}
}

# --- Main -----------------------------------------------------------------------------------------

function Get-ChainThumbprints($certificate) {
# The certificates Windows chains this one through, or $null if that can't be worked out.
try {
$chain = New-Object Security.Cryptography.X509Certificates.X509Chain
$chain.ChainPolicy.RevocationMode = [Security.Cryptography.X509Certificates.X509RevocationMode]::NoCheck
if ($chain.ChainPolicy.PSObject.Properties['DisableCertificateDownloads']) {
$chain.ChainPolicy.DisableCertificateDownloads = $true
}
[void] $chain.Build($certificate)
$thumbprints = @($chain.ChainElements | ForEach-Object { $_.Certificate.Thumbprint })
if ($thumbprints.Count -eq 0) { return $null }
return , $thumbprints
}
catch {
return $null
}
}

try {
$keys = @(Get-DeviceKey)
}
catch {
Write-Warning "The Wi-DAS device key is there but couldn't be opened, so nothing could be identified: $($_.Exception.Message)"
exit 1
}
if ($keys.Count -eq 0) {
Write-Output "There's no Wi-DAS device key for $env:USERNAME, so the Wi-DAS SDK installed nothing here for this user."
exit 0
}
$keyBytes = New-Object 'System.Collections.Generic.List[byte[]]'
foreach ($key in $keys) {
$bytes = Get-KeyPublicBytes $key
if ($null -ne $bytes) { $keyBytes.Add($bytes) }
}

$personal = New-Object Security.Cryptography.X509Certificates.X509Store 'My', 'CurrentUser'
$personal.Open('ReadWrite')
try {
$ours = @($personal.Certificates | Where-Object { (Test-CertifiesAnyKey $_ $keyBytes) -and (Test-IssuedFor $_ $ClientId) })

if ($ours.Count -eq 0) {
Write-Output "No Wi-DAS certificate for $($ClientId -join ', ') was found for $env:USERNAME."
}
else {
$realms = @($ours | ForEach-Object { Get-Realm $_ } | Where-Object { $_ } | Sort-Object -Unique)
if (@($ours | Where-Object { -not (Get-Realm $_) }).Count -gt 0) {
Write-Failure 'At least one certificate has no readable realm, so its Wi-Fi profile was not looked for.'
}
if ($realms.Count -gt 0) {
Remove-PasspointProfiles $realms
}

# Which CA issued each certificate, worked out while they're still in the store.
$issuers = @{}
foreach ($certificate in $ours) {
$chain = Get-ChainThumbprints $certificate
if ($null -ne $chain -and $chain.Count -gt 1) { $issuers[$certificate.Thumbprint] = $chain[1] }
}

foreach ($certificate in $ours) {
if ($PSCmdlet.ShouldProcess("certificate $($certificate.Subject) ($($certificate.Thumbprint))", 'Remove')) {
try {
$personal.Remove($certificate)
Write-Output "Removed the certificate $($certificate.Subject) ($($certificate.Thumbprint))."
}
catch {
Write-Failure "Couldn't remove the certificate $($certificate.Thumbprint): $($_.Exception.Message)"
}
}
}
}

# What's left in the personal store: read back after removing, or as planned under -WhatIf.
$planned = @($ours | ForEach-Object { $_.Thumbprint })
$personal.Close()
$personal.Open('ReadWrite')
if ($WhatIfPreference) {
$remaining = @($personal.Certificates | Where-Object { $planned -notcontains $_.Thumbprint })
$stuck = 0
}
else {
$remaining = @($personal.Certificates)
$stuck = @($remaining | Where-Object { $planned -contains $_.Thumbprint }).Count
}

if ($stuck -gt 0) {
# The CA and the key still serve a certificate that wouldn't go, and a re-run needs the key to
# find it again.
Write-Output 'Kept the issuing CA and the device key, because a Wi-DAS certificate is still installed.'
}
else {
# The issuing CA: only the copy the SDK added, only if it issued a certificate removed here, and
# only once nothing left in the personal store chains through it.
if ($ours.Count -gt 0) {
$intermediate = New-Object Security.Cryptography.X509Certificates.X509Store 'CA', 'CurrentUser'
$intermediate.Open('ReadWrite')
try {
$marked = @($intermediate.Certificates | Where-Object { [string]::Equals($_.FriendlyName, $IssuingCaMarker, [StringComparison]::Ordinal) })
foreach ($ca in $marked) {
$issuedOurs = @($ours | Where-Object {
(Test-BytesEqual $_.IssuerName.RawData $ca.SubjectName.RawData) -and $issuers[$_.Thumbprint] -eq $ca.Thumbprint
}).Count -gt 0
if (-not $issuedOurs) { continue }

$stillNeeded = $false
foreach ($other in $remaining) {
if (Test-BytesEqual $other.IssuerName.RawData $ca.SubjectName.RawData) { $stillNeeded = $true; break }
$chain = Get-ChainThumbprints $other
if ($null -eq $chain -or $chain -contains $ca.Thumbprint) { $stillNeeded = $true; break }
}
if ($stillNeeded) { continue }

if ($PSCmdlet.ShouldProcess("issuing CA $($ca.Subject) ($($ca.Thumbprint))", 'Remove')) {
try {
$intermediate.Remove($ca)
Write-Output "Removed the issuing CA $($ca.Subject) ($($ca.Thumbprint))."
}
catch {
Write-Failure "Couldn't remove the issuing CA $($ca.Thumbprint): $($_.Exception.Message)"
}
}
}
}
finally { $intermediate.Close() }
}

# The device key, only on request and only when no certificate left still uses it.
if ($IncludeDeviceKey) {
$inUse = @($remaining | Where-Object { Test-CertifiesAnyKey $_ $keyBytes }).Count -gt 0
if ($inUse) {
Write-Output 'Kept the Wi-DAS device key, because another Wi-DAS certificate for this user still uses it.'
}
else {
foreach ($key in $keys) {
if ($PSCmdlet.ShouldProcess("Wi-DAS device key ($($key.Provider))", 'Delete')) {
try {
$key.Delete()
Write-Output "Deleted the Wi-DAS device key ($($key.Provider))."
}
catch {
Write-Failure "Couldn't delete the Wi-DAS device key ($($key.Provider)): $($_.Exception.Message)"
}
}
}
}
}
}
}
finally {
$personal.Close()
}

if ($script:failures -gt 0) {
Write-Warning "$($script:failures) item(s) couldn't be checked or removed. See the warnings above."
exit 1
}
exit 0

It runs on Windows PowerShell 5.1, which every supported version of Windows includes, and on PowerShell 7. It reads Wi-Fi profiles through Windows' own Wi-Fi API, which it compiles with Add-Type when it runs. PowerShell's Constrained Language Mode blocks that and the certificate checks, so on a locked-down machine the script removes nothing and exits with 1.

Try it on one machine first​

Sign in as a user who had your app, and run it with -WhatIf. It lists what it would remove and changes nothing:

.\Remove-WiDasArtefacts.ps1 -ClientId client-acme -WhatIf

Then run it without -WhatIf. It prints each thing it removes, and exits with:

Exit codeMeaning
0Done, or there was nothing to remove
1Something couldn't be checked or removed, or PowerShell is in Constrained Language Mode. The warnings say what
2No Client ID was set

A 1 usually means the WLAN AutoConfig service wasn't running, so Windows couldn't list the Wi-Fi profiles. The script still removes the certificates, so a second run won't find that profile again. Delete it by name instead, with netsh wlan delete profile name="<friendlyName>".

If a certificate won't come out, the script keeps the issuing CA and the device key, so a second run can still find it.

Run it with Intune​

  1. In the Intune admin center, go to Devices → Scripts and remediations → Platform scripts → Add → Windows 10 and later.
  2. Upload the script, with your Client ID set as the default in its param block. Platform scripts can't take parameters.
  3. Set Run this script using the logged on credentials to Yes, so it runs as each user rather than as SYSTEM.
  4. Set Run script in 64 bit PowerShell Host to Yes.
  5. Assign it to the users whose machines you're clearing.

Intune reports the exit code for each user. A 1 shows as a failure, with the warnings in the script's output.

Run it with another tool​

Anything that can run PowerShell as the signed-in user works, such as a Group Policy logon script or a remote management tool's user-context task. Running it as SYSTEM, or as an administrator on someone else's behalf, only clears that account's own artefacts, so it finds nothing.

powershell.exe -File passes every argument as a single string, so it can only take one Client ID. For several, use -Command:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& .\Remove-WiDasArtefacts.ps1 -ClientId 'client-acme','client-acme-kiosk'"