Is there an easy and fast way to find out if the local computer is joined to a domain with PowerShell?
I find lots of stuff about getting the current workgroup OR domain but no clear indications on how to know if it really is a workgroup or domain. Just want to find this out before calling some Active Directory related stuff that only gives a timeout after a long wait on workgroup computers.
The [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
method takes a long time before failing on workgroup computers and the WMI class Win32_ComputerSystem
shows the name but not if it is a real domain.
use echo %logonserver% command and check the output. If it is the local machine then you are using a local account and if it is a DC that is mentioned then you are using a domain user. Another option is to use whoami command and: If you are logged using a local account then you will get as a result Computer\username.
Use Get-WmiObject to get domain name of a computer using PowerShell. Using Get-AdDomainController get domain name in active directory. wmic and SystemInfo command-line tool are useful to get domain name in cmd.
Win32_ComputerSystem
has a PartOfDomain
property that indicates whether the computer is domain joined or not. There is also a workgroup property - that should be blank if the computer is on a domain.
Example:
if ((gwmi win32_computersystem).partofdomain -eq $true) { write-host -fore green "I am domain joined!" } else { write-host -fore red "Ooops, workgroup!" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With