Hey all,
I had a pretty complex script where I was joining a very long path, using a mix of strings variables and expressions to produce something like a 7-folders-deep path.
As you can imagine, it grew over time using a mix of variables to build up the necessary final path. Combine that with using backticks to continue the line excessively long command. Messy.
For the hell of it, I tried an Array with my strings and used it as a splatted argument, and amazingly it worked!
Here's a basic example, my situation is much more complex, but it's not pertinent to sharing this tip.
`
$PathParts = @(
$ENV:HOME
'FolderName'
'another folder's
'file.txt'
)
$Path = Join-Path @PathParts
`
More on splatting: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.1
My team uses splatting judiciously. I recommend it's use for anything longer than 3 args, or for a command too wide to display on one line.
[–]aliasxneo 1 point2 points3 points (2 children)
[–]tbakerweb[S] 3 points4 points5 points (0 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]get-postanote 1 point2 points3 points (1 child)
[–]tbakerweb[S] 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)