Skip to content

Commit 3450f22

Browse files
committed
tip: Fix tip example code
1 parent 0d3b6c5 commit 3450f22

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/PowerShellTips/2025-05-05-use-join-path-and-split-path-to-create-cross-platform-paths.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ $tip = [tiPS.PowerShellTip]::new()
22
$tip.CreatedDate = [DateTime]::Parse('2025-05-05')
33
$tip.Title = 'Use Join-Path and Split-Path to create cross-platform paths'
44
$tip.TipText = @'
5-
When creating file paths in PowerShell, use the `Join-Path` cmdlet instead of string concatenation. This ensures that the correct path separator is used for the current platform (e.g. `\` on Windows and `/` on Linux/macOS). PowerShell 6 introduced the -AdditionalChildPaths parameter, which allows you to specify multiple child paths to join.
5+
When creating file paths in PowerShell, use the `Join-Path` cmdlet instead of string concatenation. This ensures that the correct path separator is used for the current platform (e.g. `\` on Windows and `/` on Linux/macOS). PowerShell 6 introduced the -AdditionalChildPath parameter, which allows you to specify multiple child paths to join.
66
77
Similarly, you can use the `Split-Path` cmdlet to split a path into its components. This is useful for extracting the directory or file name from a full path.
88
'@
@@ -15,9 +15,12 @@ $tip.Example = @'
1515
[string] $configFilePath = Join-Path $configDirectoryPath 'config.json' # Excludes -Path and -ChildPath for brevity.
1616
1717
# In PowerShell 6+ you can join multiple child paths at once.
18-
[string] $configFilePath = Join-Path -Path $HOME -AdditionalChildPaths 'Config', 'config.json'
18+
[string] $configFilePath = Join-Path -Path $HOME -AdditionalChildPath 'Config' 'config.json'
1919
[string] $xmlFilePath = Join-Path $HOME 'Config' 'config.xml' # Excludes parameter names for brevity.
2020
21+
# Use -Resolve to ensure we get an absolute path, and error if the path does not exist.
22+
[string] $configFilePath = Join-Path $HOME 'Config' 'config.json' -Resolve
23+
2124
# Get the name of the file with and without the extension, it's parent directory path, and it's parent directory name.
2225
[string] $fileName = Split-Path -Path $configFilePath -Leaf
2326
[string] $fileNameWithoutExtension = Split-Path -Path $configFilePath -LeafBase

0 commit comments

Comments
 (0)