Peter Kronenberg
Peter Kronenberg

Reputation: 1296

Putting Powershell variable in double quotes shows Object type instead of value

I see this problem in several area, but here is an example

I read an xml document like this and print out a value

[xml]$pom = get-content -path pom.xml
PS C:\> $pom.project.artifactId
nexus-peter-test-service

However, if I put the value in double quotes, I get this

"$pom.project.artifactId"
System.Xml.XmlDocument.project.artifactId

I need the value in double quotes because it's part of a long string. In my case, a url. So I'm using it like this:

"/$pom.project.artifactId/"

Why does Powershell change the meaning of the variable when in it's double quotes? And how can I fix this?

Upvotes: 0

Views: 820

Answers (1)

Mark Reed
Mark Reed

Reputation: 95242

The problem is that the interpolation stops at the period. It interpolates "$pom" - which stringifies as the class name - followed by the literal string ".project.artifactId".

To interpolate anything more complex than a simple variable name, you need to wrap $(...) around the expression:

"$($pom.project.artifactId)"

Upvotes: 4

Related Questions