Pipeline variable expansion within double quotes

I have a simple script backing up config files for a couple of programs and everything works pretty much as i want it to, but I’m a little surprised by one thing.
I have PSCustomObjects for each config file and each object contains a Name, Path and Destination variable. The script uses a Foreach-Object to iterate over an array containing the objects and as part of the Foreach-Object loop it checks if the destination file already exists, and if so it writes a warning that I’ve already backed up the file once today.

I had to write the warning in two lines like this:

$Warning = $_.Name + '-file has already been backed up once today!'
Write-Warning $Warning

I thought I would be able to use simple variable expansion like this to

Write-Warning "$_.Name file has alread been backed up once today!"

But using that syntax Powershell expands the entire PSCustomObject into the text rather than just the Name variable. Am I missing something or is that expected behavior?

When referencing an object’s member (property or method) in string such as you are, you need to use a subexpression to do so:

Write-Warning "$($_.Name) file has already been backed up once today!"

Thank you!

I’m pretty sure I’ve bumped into a version of this previously but managed to forget about it again.

/KLaage

I always use string format:

Write-Warning ("{0} file has alread been backed up once today!" -f $_.Name)