To get the domain name with PowerShell, read the Domain property of the Win32_ComputerSystem class. It works the same in PowerShell 7 and Windows PowerShell 5.1:
(Get-CimInstance -ClassName Win32_ComputerSystem).Domain
In this tutorial, I’ll show you how to get the domain name in PowerShell, check whether a PC is joined at all, build the full computer name (FQDN) and handle workgroup machines.
The sample check we’ll build
Picture a help desk in Denver that gets a ticket from a laptop that can’t reach the file server. The first question is simple: is this machine even on the domain?
Win32_ComputerSystem answers that, and it also holds the computer name in PowerShell. Here are the five properties that matter:
Get-CimInstance -ClassName Win32_ComputerSystem |
Select-Object Name, Domain, PartOfDomain, Workgroup, DomainRole |
Format-List
Output:
Name : DESKTOP-4T7K2QX
Domain : WORKGROUP
PartOfDomain : False
Workgroup : WORKGROUP
DomainRole : 0

My test PC isn’t joined to a domain, so you see WORKGROUP. I replaced its real name with DESKTOP-4T7K2QX. On a domain-joined laptop, Domain shows the domain name instead, like corp.example.com.
Method 1: Get the domain name with Get-CimInstance
When you only need the name, expand the one property, as in the quick answer above:
(Get-CimInstance -ClassName Win32_ComputerSystem).Domain
Output:
WORKGROUP
How does this code work? Get-CimInstance queries the WMI class Win32_ComputerSystem, which describes the local Windows computer. The parentheses run the query first, then .Domain picks one property.
Here’s the catch. Microsoft documents that Domain returns the workgroup name when the computer isn’t part of a domain, so a string check alone can’t tell you whether the PC is joined.
Pro Tip: add -ComputerName to ask another machine, for example Get-CimInstance Win32_ComputerSystem -ComputerName PC-0142. That needs WinRM access to the remote PC, which I didn’t set up for this post.
How do I check if a computer is joined to a domain?
Use PartOfDomain, which is True only on a domain member. DomainRole adds the machine’s role, from 0 (standalone workstation) to 5 (primary domain controller). This Get-DomainStatus function combines both:
function Get-DomainStatus {
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
$role = switch ($cs.DomainRole) {
0 { 'Standalone workstation' }
1 { 'Member workstation' }
2 { 'Standalone server' }
3 { 'Member server' }
4 { 'Backup domain controller' }
5 { 'Primary domain controller' }
}
if ($cs.PartOfDomain) {
"$($cs.Name) is joined to $($cs.Domain) ($role)"
}
else {
"$($cs.Name) is in workgroup $($cs.Workgroup) ($role)"
}
}
Get-DomainStatus
DESKTOP-4T7K2QX is in workgroup WORKGROUP (Standalone workstation)

How does this code work? A switch statement in PowerShell turns the DomainRole number into a label. The if block then picks Domain or Workgroup based on PartOfDomain.
Both versions returned the same line. To change the result, see how to add a computer to a domain with PowerShell or remove a computer from a domain.
Method 2: Use $env:USERDNSDOMAIN and $env:USERDOMAIN
Windows also stores domain names in environment variables. They’re instant, but they describe the signed-in user, not the computer:
"USERDNSDOMAIN: [$env:USERDNSDOMAIN]"
"USERDOMAIN: [$env:USERDOMAIN]"
"Signed in as: $([Environment]::UserDomainName)\$([Environment]::UserName)"
if ([string]::IsNullOrEmpty($env:USERDNSDOMAIN)) {
'This session did not sign in with a domain account.'
}
Output:
USERDNSDOMAIN: []
USERDOMAIN: [DESKTOP-4T7K2QX]
Signed in as: DESKTOP-4T7K2QX\alice
This session did not sign in with a domain account.
How does this code work? USERDNSDOMAIN holds the user’s DNS domain, such as CORP.EXAMPLE.COM, and is only set after a domain sign-in. USERDOMAIN holds the short (NetBIOS) name, like CORP.
I signed in with a local account, so USERDNSDOMAIN is empty and USERDOMAIN falls back to the computer name. The brackets make the empty value visible, and the IsNullOrEmpty check catches it.
Keep that difference in mind. A local admin on a domain PC still gets the computer name in USERDOMAIN, so use Win32_ComputerSystem when you mean the machine.
Method 3: Get the computer’s FQDN with PowerShell
The fully qualified domain name (FQDN) is the host name plus the DNS domain, like PC-0142.corp.example.com. .NET’s IPGlobalProperties class gives you both parts:
$ip = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
"Host name: $($ip.HostName)"
"Domain name: [$($ip.DomainName)]"
if ($ip.DomainName) { $fqdn = "$($ip.HostName).$($ip.DomainName)" }
else { $fqdn = $ip.HostName }
"FQDN: $fqdn"
"DNS lookup: $([System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).HostName)"
Output:
Host name: DESKTOP-4T7K2QX
Domain name: []
FQDN: DESKTOP-4T7K2QX
DNS lookup: DESKTOP-4T7K2QX
How does this code work? DomainName returns an empty string when t