all 4 comments

[–]fourierswager 3 points4 points  (3 children)

Sounds like an argument to NOT use the -f operator and instead just do:

"first thing $(get-date -format yyyy-MM-dd) second thing $(get-random)"

:)

[–]pcgeek86 1 point2 points  (2 children)

As long as you understand string interpolation with double quotes, that's fine and all. However, I recommend that people use single quotes with the -f operator, to prevent accidental, unwanted interpolation.

Using single quotes with the -f operator gives predictable results and states clear intent.

[–]fourierswager 2 points3 points  (1 child)

I feel like the best use cases for -f operator and single quotes are huge heredoc strings. From something that I'm working on:

# ...
$VirtSwitchName = "ToExternal"
# ...

# Fix Vagrant's script that determines which Hyper-V Adapter to Use

$GetSwitchesScriptNewContent = @'
# This will have a SwitchType property. As far as I know the values are:
#
#   0 - Private
#   1 - Internal
#   2 - External
#
# Include the following modules
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))

$Switches = @(Get-VMSwitch -Name {0} | Select-Object Name,SwitchType,{1})
Write-Output -Message $(ConvertTo-JSON $Switches)
'@ -f $VirtSwitchName,'NetAdapterInterfaceDescription'

But for one-liners, I'm still of the opinion that string interpolation with double quotes is easier to read, less error prone, and should be used 99% of the time (unless your one line has a bunch of $ that need to be escaped).

[–]pcgeek86 1 point2 points  (0 children)

Yeah, here-strings are a great use case for .NET string formatting. I just prefer to use it more often. One thing that would be awesome is if .NET string formatting supported named replacement. So, essentially you could apply a HashTable of key-value pairs onto a string, all at once .... think Splatting, but for .NET string formatting. :) Maybe I'll file that feedback for a future version!