0

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?

2
  • Did you try to use a subexpression? "$($pom.project.artifactId)" Commented Jul 30, 2021 at 20:43
  • PowerShell starts to do variable expansion inside strings from left to right. If it expanded successfully $pom it will assume the rest of it is a string Commented Jul 30, 2021 at 20:45

1 Answer 1

4

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)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.