Description
Is there a best practice way of handling file path formatting? These are the ways I know how to format them right now, assuming a path of '\Server1\Share1\SubFolder1\File1.txt'
$Server = 'Server1'
$Share = 'Share1'
$SubFolder = 'SubFolder1'
$File = 'File1.txt'
Join-Path
-
Piping
$FullPath = Join-Path -Path $Server -ChildPath $Share | -ChildPath $SubFolder | -ChildPath $File
-
MultiLine
$Path1 = Join-Path -Path $Server -ChildPath $Share
$Path2 = Join-Path -Path $Path -ChildPath $SubFolder
$FullPath = Join-Path -Path $Path -ChildPath $File
-Join parameter
$FullPath = '\', $Server, $Share, $SubFolder, $File -join '\'
String interpolation, adding an underscore after the file name to show variable expansion, I know it's not valid.
$FullPath = "\\$Server\$Share\$SubFolder\$($File)_"
String formatting
$FullPath = '\\{0}\{1}\{2}\{3}' -f $Server, $Share, $SubFolder, $File
String concatenation
$FullPath = '\\' + $Server + '\' + $Share + '\' + $SubFolder + '\' + $File
.NET class
[io.path]::combine("\\$Server", $Share, $SubFolder, $File)