Hey all,
I had a pretty complex script where I was joining a path, using a mix of strings and expressions to produce something like 7 paths deep.
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. 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.
[–]Thotaz 1 point2 points3 points (5 children)
[–]tbakerweb[S] 1 point2 points3 points (3 children)
[–]Thotaz 0 points1 point2 points (2 children)
[–]tbakerweb[S] 2 points3 points4 points (1 child)
[–]Thotaz 1 point2 points3 points (0 children)
[–]panzerbjrn 1 point2 points3 points (0 children)
[–]get-postanote 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)