Check if a system is joined to domain using PowerShell

Knowing whether a Windows system is joined to a domain or not can be helpful in various situations, such as troubleshooting network connectivity or determining the level of system access. With PowerShell, you can easily check if a system is joined to a domain in just a few lines:

 

$status = (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain
if ($status) {
“This computer is joined to the domain $((Get-WmiObject -Class Win32_ComputerSystem).Domain).”
}
else {
“This computer is not joined to a domain.”
}

First, the script uses the Get-WmiObject cmdlet to retrieve information about the computer system using the Win32_ComputerSystem class. Specifically, it retrieves the value of the “PartOfDomain” property, which indicates whether the computer is joined to a domain or not. The result is stored in the “$status” variable.

Next, the script evaluates the value of the “$status” variable using an “if” statement. If the value is true, it means that the computer is joined to a domain, and the script executes the code block within the “if” statement.

Inside the “if” block, a message is constructed using string interpolation. It incorporates the domain name, obtained again using the Get-WmiObject cmdlet and the “Domain” property from the Win32_ComputerSystem class. The message is then displayed, indicating that the computer is joined to the domain.

If the value of the “$status” variable is false, indicating that the computer is not joined to a domain, the script executes the code block within the “else” statement.

Inside the “else” block, a simple message is displayed, stating that the computer is not joined to a domain.

Leave a comment

Your email address will not be published. Required fields are marked *

one × 3 =