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?