MyITGuy
12/11/2017 - 8:42 PM

PowerShell: Test-UserAccount

PowerShell: Test-UserAccount

function Test-UserAccount {
	begin {
		$AssemblyName = "System.DirectoryServices.AccountManagement"
		$Assembly = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where {$_ -match $AssemblyName}
		if (!$Assembly) {
     		Write-Verbose "Loading assembly, $($AssemblyName).";
			$Assembly = [System.Reflection.Assembly]::LoadWithPartialName($AssemblyName)
		}
	}
	process {
		if (!$Assembly) {
			Throw "$($AssemblyName) could not be loaded."
			return
		}
		try {
			$CurrentUserPrincipal = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
			$IsUserPrincipal = ($CurrentUserPrincipal -is [Object])
		} catch {
			$IsUserPrincipal = $false
		}
		return $IsUserPrincipal
	}
	
	end {
	}
}