Parse and build paths with Split-Path and Join-Path

Stop slicing strings with .Substring() — PowerShell has dedicated cmdlets to pull apart and reassemble file paths.

WindowsFilesscriptingpowershellLow risk
Use Split-Path <path> -Leaf for the filename, -Extension for '.csv', plain Split-Path for the parent, and Join-Path <a> <b> to combine paths.

01Command

Command
$ Split-Path 'C:\Work\report.csv' -Leaf ; Split-Path 'C:\Work\report.csv' -Extension ; Join-Path 'D:\Backup' '2026\logs'

02What it does

These two cmdlets are forgotten because people hard-code '\' string surgery, which breaks on UNC paths and registry keys. Split-Path also supports -Resolve to verify the parent actually exists, and pipelines naturally from Get-ChildItem. Join-Path inserts the correct separator for the active provider and handles a trailing slash on the parent gracefully.

03Risk

RiskLow risk
RequirementsPowerShell

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • Join-Path | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents Join-Path combining a parent path and child path with the provider's correct separator.
  • Split-Path | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents Split-Path with -Parent, -Leaf, -Extension, -NoQualifier, -Qualifier and -Resolve parameters.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions