a. gc env:Computername
$computer = get-content env:ComputerName
Use the WMI objects to get computer name and other details
$Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer -namespace “rootCIMV2”
for Machine domain: $computer.Domain
# Print Computer Name
“Computer Name is: {0}” -f $Computer.Name
If you want to get the User Domain, you can run
gc env:UserDNSDomain – Fully quslified Domain name
or gc env:UserDomain – short domain
[
%COMPUTERNAME% for comuter name
%USERDNSDOMAIN for full qualified User Domain
%USERDOMAIN% for short domain
]
Note: To display all the environment variables of particular instance
Run set in the command prompt
>set
Similarly, you can run
>dir env: | sort Name
Getting the Computer Name, Domain, more using powershell
Getting the Computer Name, Domain, more using powershell