Specifying Current Domain Name of Local Machine in PowerShell

If a PowerShell script requires the input of a domain name after a parameter, is there a way to just generically specify the domain name of the local machine that you're on?  

New-ADGroup -Server localhost:60000 -Path "OU=AccountDeptOU,DC=AppNC" -Name AccountLeads -GroupScope DomainLocal  -GroupCategory Distribution

In this example script the "path" parameter requires the value of DC (Domain Controller) to be AppNC.  If I was going to take this script to other domain environments then it obviously won't work because the domain names would be different.

Is there some kind of value I can put instead to just say,"This local machine's current domain name." If that makes any sense.  

I'd appreciate any insight.  Thanks :)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
you could use $env.logonserver
emag50Author Commented:
So the new syntax would look something like this instead?  I'm trying different things and rearranging the syntax but I can't get it to work.  

New-ADGroup -Server localhost:60000 -Path "OU=AccountDeptOU,DC=$env.logonserver" -Name AccountLeads -GroupScope DomainLocal  -GroupCategory Distribution

I'm also trying it with the different examples that Microsoft has on their technet site but still no luck.  Only works when I explicitly define the domain names' prefix and suffix (e.g. contoso.test would be dc=contoso,dc=com) as shown below:

New-ADGroup -Name TestGroup -SamAccountName Administrators -GroupCategory Security -GroupScope Global -DisplayName TestGroup -Path "CN=TestOU,DC=Contoso,DC=Com" -Description "Members of this group are Test Users"

Not quite sure how to replace the bold portion in a fashion that will give me the current domain name value.  

FYI I'm just trying to create a new group to place in a specific OU but this info doesn't matter much.  Thanks for your input.
David Johnson, CD, MVPOwnerCommented:
$dn = get-domain $DNSName
New-ADGroup -Name TestGroup -GroupCategory Security -GroupScope Global -DisplayName TestGroup -Path $dn -Description "Members of this group are Test Users"

 function Get-Domain {
 #Retrieve the Fully Qualified Domain Name if one is not supplied division.domain.root
 $fqdn = $env:logonserver
 $FQDNArray = $FQDN.split(".")
 # Add A Separator of ','
 $Separator = ","
 for ($x = 0; $x -lt $FQDNArray.Length ; $x++)
 #If it's the last item in the array don't append a ','
 if ($x -eq ($FQDNArray.Length - 1)) { $Separator = "" }
 [string]$DN += "DC=" + $FQDNArray[$x] + $Separator
 return $DN

Open in new window

"DC" in a distinguished name does not stand for "Domain Controller", it stands for "Domain Component".
You can use something like this:
"OU=AccountDeptOU,DC=$($ENV:USERDNSDOMAIN.Replace('.', ',DC='))"

Open in new window

Or to make it easier to read in a script:
$DomainComponent = "DC=$($ENV:USERDNSDOMAIN.Replace('.', ',DC='))"
New-ADGroup -Server localhost:60000 -Path "OU=AccountDeptOU,$DomainComponent" -Name AccountLeads -GroupScope DomainLocal  -GroupCategory Distribution

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emag50Author Commented:
This worked perfectly.  Thanks a lot!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.