Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active November 14, 2017 03:23
Show Gist options
  • Save techthoughts2/4f249d49476eec92c691 to your computer and use it in GitHub Desktop.
Save techthoughts2/4f249d49476eec92c691 to your computer and use it in GitHub Desktop.
Tests to verify that the script is running as a domain user or local user
<#
.Synopsis
Verifies if currently logged in user is a local user or domain user
.DESCRIPTION
This function will evaluate the users domain and determine if the current user is a domain user or local user. If true is returned the user is a domain user, if false is returned the user is a local user.
.EXAMPLE
Test-DomainUser
This will return a true/false indicating if the user is a domain user or not
.EXAMPLE
Test-DomainUser -Verbose
This will return a true/false indicating if the user is a domain user or not. Verbose output will be shown.
.OUTPUTS
Boolean value
.NOTES
Author: Jake Morrison
http://techthoughts.info
#>
function Test-DomainUser {
[CmdletBinding()]
Param()
[bool]$domainUser = $true #assume they are
Write-Verbose -Message "Getting current domain of user..."
$dom = $env:USERDOMAIN.ToUpper()
Write-Verbose -Message "Domain: $dom"
Write-Verbose -Message "Getting hostname..."
$hostName = hostname
$hostName = $hostName.ToUpper()
Write-Verbose -Message "Hostname: $hostName"
Write-Verbose -Message "Evaluating if domain user or local user..."
if ($dom -eq $hostName) {
Write-Verbose "Context user: $dom\$env:USERNAME"
Write-Verbose "User is a local user"
$domainUser = $false
}
else {
Write-Verbose "Context user: $dom\$env:USERNAME"
Write-Verbose "User is a domain user"
}
return $domainUser
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment