How to check if the computer is connected to a domain using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To check if a computer is connected to any domain we can use multiple methods. In this article, we can use two methods. One using the System Information of the computer and the second using the DirectoryServices .Net Class.

First method using System Information and filter out a string called “Domain” which shows us if the computer is in the domain or the workgroup.

systeminfo | findstr "Domain"

Output

If the computer is in the workgroup, It will show the workgroup name. For example,

In the second method, we will use the directory service .Net class method name GetComputerDomain(). If the server is not connected to the domain then this command will throw an error.

For the server connected to a domain,

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()

Output

Forest                  : labdomain.local
DomainControllers       : {ADDC.labdomain.local}
Children                : {}
DomainMode              :
Parent                  :
PdcRoleOwner            : ADDC.labdomain.local
RidRoleOwner            : ADDC.labdomain.local
InfrastructureRoleOwner : ADDC.labdomain.local
Name                    : labdomain.local

You can filter out the domain name using Name Property.

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain() | Select  Name

Output

Name
----
labdomain.local

If the server is not connected to the domain then, it will throw an error saying the local computer is not joined to any domain as shown below.

raja
Published on 28-Dec-2020 11:33:41
Advertisements