Split-Path -Parent / currently broken on Unix
See original GitHub issueSteps to reproduce
Split-Path -Parent /
Expected behavior
The empty string (current behavior on Windows), but, ideally, / - see #4131
Actual behavior
An error occurs.
Split-Path : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
Environment data
PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
'Split-Path "/" -Leaf' returns empty string instead of "/ ...
On Unix, Split-Path / -Parent is currently broken, as you reported in 'Split-Path "/" -Parent' fails on Ubuntu1804/Linux/Wsl #14392, which is a ...
Read more >How to split path by last slash? - bash
I need to write a bash script that processes one path at a time, splits it at the last slash and then launches...
Read more >Split-Path - PowerShell
The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It...
Read more >How to Split Paths with the PowerShell Split-Path Cmdlet
Learn how to split and return different elements of a path using the PowerShell Split-Path cmdlet in this tutorial.
Read more >Powershell split-path formatting
This should work regardless of how many sub folders are in the path: $result = $ItemDirectory While (($result -split '\\' | ? {...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@PowerShell/powershell-committee reviewed this. Expectation is that scripts using the current Windows PowerShell behavior to parse a file path would need to special case Unix if we have non-symmetric behavior. For example:
This loops forever since the ending state is never hit. To support the same scripts working on Windows and Unix, we need to fix the behavior on Unix so that:
split-path -leaf / => returns / split-path -parent / => returns “”
I think it’s fine to not have symmetry for the sake of symmetry. On Unix, if the expectation is that the
-Leafand-Parentof / is/, then I think that would be ok and just documented. On Windows, I don’t think we’ll change it as it would be a breaking change w/o clear benefit to warrant the change. Will have @PowerShell/powershell-committee review.