Private/Test-AGLowPrivUserOwned.ps1

function Test-AGLowPrivUserOwned {
    <#
    .SYNOPSIS
        Tests whether the low privileged lab user exists and was created by ADCSGoat.
 
    .DESCRIPTION
        Internal helper. A user is ours when its description carries the
        'Generated by ADCSGoat' marker. A missing user, or one without the
        marker, is not ours. Uninstall-ADCSGoat uses this to leave anything it
        did not create, including that account's RDP access, untouched.
 
    .PARAMETER UserName
        The sAMAccountName of the user.
 
    .PARAMETER Server
        The domain controller to query. Defaults to the logon server.
 
    .OUTPUTS
        System.Boolean.
 
    .EXAMPLE
        Test-AGLowPrivUserOwned -UserName user -Server dc.adcs.goat
    #>

    [CmdletBinding()]
    [OutputType([bool])]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$UserName,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Server
    )

    Add-Type -AssemblyName System.DirectoryServices

    $serverPrefix = if ($Server) { "LDAP://$Server/" } else { 'LDAP://' }
    $rootDSE = New-Object System.DirectoryServices.DirectoryEntry("${serverPrefix}RootDSE")
    $defaultNC = "$($rootDSE.defaultNamingContext)"

    $searcher = New-Object System.DirectoryServices.DirectorySearcher
    $searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("$serverPrefix$defaultNC")
    $searcher.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=$UserName))"
    $null = $searcher.PropertiesToLoad.Add('description')
    $result = $searcher.FindOne()

    $null -ne $result -and "$($result.Properties['description'][0])" -like '*Generated by ADCSGoat*'
}