Author Posts

September 10, 2014 at 9:21 am

I am using get-adcomputer to get the name and dnshostname of servers.
The dnshostname value is
I would like to change this to just the value in my output, not

Struggling, testing with only one server, I have tried to get the computername and domain name only (as opposed to the fqdn) into two columns:

$computerdetails = Get-ADComputer -server $dc -Filter {Name -like "servername*" } -Property * | select @{N="computername";E={$}}`

The computername works ok, but the dnsdomain is empty.

Can someone advise me please?

September 10, 2014 at 9:47 am

select @{n="ComputerName";e={$}},@{n="dnsdomain";e={$_.DNSHostName.Replace("$($","")}}

September 10, 2014 at 9:49 am

haha I'm answering my own questions today it seems.
I did this and it worked:

$computerdetails = Get-ADComputer -server $DC -Filter {Name -like "servername*" } -Property * | select @{N="computername";E={$}}`
,@{N="dnsdomain";E={$_.DNSHostName.Split(".")[1] + "." + $_.DNSHostName.Split(".")[2]}}


September 10, 2014 at 9:51 am

Thanks! In the meantime I used split, but Jason's solution worked as well 🙂

September 10, 2014 at 9:59 am

Here's another option. Note that you don't need to use -Properties * as the properties you're looking for are returned by Get-ADComputer by default:

Get-ADComputer -server $dc -Filter {Name -like 'servername*'} |
Select-Object -Property @{name='computername';expression={$}},
@{name='dnsdomain';expression={$_.DNSHostName -Replace '^(.*?)\.'}}