How to split paths in PowerShell
Overview
In PowerShell, we use the Split-Path cmdlet to get any part of the path, like the parent folder, child folder, or files.
Syntax
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Parameters
Path: It accepts a path to split.Leaf: It returns the last part of the given path.Resolve: This parameter references the files that the split path leads to.
Return value
This returns a part of the path as a string.
Let's look at an example.
Example
In the example below, we'll create a test folder and some files in it and display them using the split path.
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Explanation
In the above code snippet:
- Line 4: We get the parent container of the given path using the cmdlet
Split-Path. - Line 7–10: We create a folder
testin the current directory and create some test files in it using the cmdletNew-Item. - Line 15: We display the files for the given path using the cmdlet
Split-Pathand the parametersLeafandResolve. TheLeafpoints to the last part of the path andResolvedisplays the file names instead of thePath.