As a system administrator, we need to install application on remote computer on the domain. As a best practice its good to get computer domain membership and if computer is on domain then performs installation or other process.
In this article, I will explain you how to get computer domain membership using PowerShell script.
Get-WmiObject
class in PowerShell management library has many important properties to check computer is part of domain or not, check services on local computer, remote computer or stop service on remote computer.
Lets get computer domain membership and check if computer is on domain or not using given below PowerShell scripts
Check if a Computer is a member of Domain or WorkGroup
Using PowerShell Get-WmiObject
class, we can check if a computer is member of domain or workgroup as below
# Check if computer part of domain (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain # Check if computer is on workgroup (Get-WmiObject -Class Win32_ComputerSystem).Workgroup
In the above PowerShell script,
first command check if a computer is on domain using Get-WmiObject
cmdlet.
PartofDomain
property of given cmdlet is Boolean type and return true or false based on computer domain membership. If computer is on domain, it will returns true else false.
Second command, check if computer is on workgroup or not. Workgroup
is string property and return workgroup name if computer is workgroup. If computer is having domain membership, it will be empty.
Output of above PowerShell script to get computer domain membership as below
You can also use $env:USERDNSDomain
environment variable in PowerShell to get domain name for computer. If returns domain name, it means computer has joined domain, else it will returns empty.
Cool Tip: How to get adcomputer operating system in PowerShell!
Conclusion
In the above article, we have learned how to get computer domain membership in PowerShell to perform further tasks.
Using Get-WmiObject PartOfDomain
property, you can check if a computer is member of domain or workgroup in PowerShell.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.