Powershell Not Expanding Variable String inside Foreach-Object

Question:

I’m trying to template some strings using Powershell string expansion. When I use a string literal it’s fine, but when I pass the string as a variable it doesn’t work. What do I need to do differently?

Answer:

You cannot do this with just a normal string. However, you could put the string inside a scriptblock and then invoke it with the call operator &:

Source:

Powershell Not Expanding Variable String inside Foreach-Object by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply