0

I'm starting to learn powershell scripting and I'm getting stuck on a simple problem (surely due to my inexperience): string interpolation.

I'm trying to print a list of directories that I'm obtaining with the Get-ChildItem cmdlet. I've read in tutorials that to output a property of an object inside a string you should use "${$variable.property}", so I tried with this code:

foreach($pluginDir in Get-ChildItem -Path "./Plugins" -Directory){
    Write-Host "Found plugin directory ${$pluginDir.FullName}"
}

The Get-ChildItem command is working correctly (I've tried by printing $pluginDir.FullName directly without interpolating it inside the string), however the string interpolation is not. All I get in the output from the above code is:

Found plugin directory
Found plugin directory
Found plugin directory
...

what am I doing wrong here?

1
  • To debug use following inside the foreach : $pluginDir | Format-Table Format table is great for enumerating through objects and displaying all the properties. The property FullName doesn't exist. Format-Table will give all the property names. Commented Jan 16, 2023 at 10:56

1 Answer 1

2

The correct syntax would be:

Write-Host "Found plugin directory $($pluginDir.FullName)"

More on command substitution: https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-string-substitutions?view=powershell-7.3#command-substitution

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works, apparently the tutorial I was following was talking s**t 😅

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.