Powershell Select-String output

Viewed 749

Let's say that the Root Domain name is contoso.com

When I run this script it gives me an output along the lines of:

xxxxxx : contoso.com

The "xxxxx" represents some characters, including the file name.

What do those characters mean, and how can I make it so that I only get "contoso.com" as the output, without the characters in front of it?

Information

I am using the following powershell script to obtain information:

Get-ADDomain >> ./log.txt

From all of the information that Get-ADDomain gives me, I want to extract the Root Domain name, so I use Select-String to get it out of the text file, as follows:

$domain = Select-String -Path ./log.txt -Pattern "RootDomain"

2 Answers

The answer to your question is that xxxxxx gives you the file name and the line number.

Once this said you can retreive RootDomain if it exists in the returnes object using

$(Get-ADDomain).RootDomain

You just have to understand that PowerShell Cmdlets returns objects with properties and methods. $(Get-ADDomain) execute the CmdLet Get-ADDomain and .RootDomain returns the value of the property RootDomain

Yes, and $() is not required, just (Get-ADDomain).RootDomain is enough. or the PowerShell way is $Domain = Get-ADDomain | Select-Object -ExpandPropery RootDomain .

Related