Table of Contents
Split Path into Array in PowerShell
Using Split()
Method
Use the Split()
method to split the path into an array in PowerShell.
1 2 3 4 5 |
$path = "C:\Users\John\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $pathIntoArray = $path.Split("\") $pathIntoArray |
1 2 3 4 5 6 7 8 |
C: Users John Desktop Samples House_Prediction_Using_Two_File_Dataset.pdf |
First, we defined a $path
variable and set its value to a path as demonstrated above; you can initialize it with your own path. Next, we chained the Split()
method with the $path
variable and passed \
as a delimiter to the Split()
method to split the $path
into an array. Finally, we stored this array in the $pathIntoArray
variable.
We can use index operator to get elements of the array.
1 2 3 4 5 6 |
$path = "C:\Users\John\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $pathIntoArray = $path.Split("\") $last = ($pathIntoArray)[-1] $last |
1 2 3 |
House_Prediction_Using_Two_File_Dataset.pdf |
For example:
To get last element in array, we can use following code:
Using -Split
Operator
Use the -Split
operator to split the path into an array in PowerShell.
1 2 3 4 5 |
$path = "C:\Users\John\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $pathIntoArray = $path -Split "\\" $pathIntoArray |
1 2 3 4 5 6 7 8 |
C: Users John Desktop Samples House_Prediction_Using_Two_File_Dataset.pdf |
This example is similar to the previous one, but we used the -Split
operator this time, which took a \
as an argument. Note that the \
must be doubled while using the -Split
operator. Also, unlike the previous code fence, we stored the array in the $pathIntoArray
variable.
You might wonder that if the -Split
operator and Split()
method perform the same thing, then why use them separately? You are correct that both split a string into an array of substrings, but some similarities and differences are listed below:
- First, they have different syntax because
Split()
is a method and-Split
is an operator; you can refer to the last two examples to understand it. - Both split the string based on the provided delimiter; however, the
-Split
operator can split based on the provided regular expressions, which makes it more powerful. - Both returns an array of strings; you can check the returned type using GetType() method. For example,
$pathIntoArray.GetType()
. - Regarding performance, the
Split()
method is faster than the-Split
operator, particularly when we split based on a single delimiter. However,-Split
can be faster when splitting based on complex patterns since it allows a regular expression pattern as a delimiter.
Now, you know the similarities and differences between the -Split
and Split()
, so choose the one which best suits your project needs.
Until now, we learned how to split the path of the specified file into an array, but what if we are supposed to split the directory and path of the given file into separate arrays? What does it mean? Suppose we have C:\Users\John\Documents\file.txt path. Splitting the path into an array will result in C:
, Users
, John
, Documents
, and file.txt
as array elements, while splitting the directory will have C:
, Users
, John
, and Documents
as array elements.
Further reading:
How to split the directory into an array? Let’s learn it in the following section.
Split Directory into Array in PowerShell
Using System.IO.Path
Class
To split the directory and path of the specified file into separate arrays:
- Store a path of a file in a variable.
- Use
GetDirectoryName()
to retrieve the directory of the given file. - Chain the
Split()
method with the path variable created in the first step to get an array of the path. - Chain the
Split()
method with the directory variable created in the second step to get an array of the directory. - Use the
Write-Host
cmdlet to print the customized outcomes.
1 2 3 4 5 6 7 8 |
$path = "C:\Users\John\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $directory = [System.IO.Path]::GetDirectoryName($path) $pathIntoArray = $path.Split("\") $directoryIntoArray = $directory.Split("\") Write-Host "A path into an array:`n$pathIntoArray" Write-Host "A directory into an array:`n$directoryIntoArray" |
1 2 3 4 5 6 |
A path into an array: C: Users John Desktop Samples House_Prediction_Using_Two_File_Dataset.pdf A directory into an array: C: Users John Desktop Samples |
In the above output, the array elements are separated by a single whitespace. So how did we get these arrays? First, we stored the path of the House_Prediction_Using_Two_File_Dataset.pdf
file in the $path
variable. Next, we used the GetDirectoryName()
method of the Path
class, provided $path
as a parameter to get the directory of the House_Prediction_Using_Two_File_Dataset.pdf
file, which we stored in the $directory
variable.
Next, we chained the Split()
method with $path
and $directory
separately to get two different arrays stored in the $pathIntoArray
and $directoryIntoArray
variables. Remember, we passed \
to the Split()
method as a delimiter. Finally, we used the Write-Host
cmdlet to display customized output on the PowerShell terminal.
`
n
is a unique character in PowerShell; we used it with theWrite-Host
cmdlet to add a new line character.
Now, think of a situation where we want complete control over array elements. For example, we want to split the path into an array having parent, leaf, and qualifier as the array elements. What are they, and how can we do it? See the following section to learn.
Using Split-Path
Cmdlet
Use the Split-Path
cmdlet to retrieve various parts of the path to form an array in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$path = "C:\Users\John\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $pathParent = Split-Path $path -Parent $pathLeaf = Split-Path $path -Leaf $pathQualifier = Split-Path $path -Qualifier $pathIsAbsolute = Split-Path $path -IsAbsolute $pathIntoArray = @("Parent of the path: $pathParent", "Leaf of the path: $pathLeaf", "Qualifier of the path: $pathQualifier", "Is the path absolute? (True/False): $pathIsAbsolute") $pathIntoArray |
1 2 3 4 5 6 |
Parent of the path: C:\Users\John\Desktop\Samples Leaf of the path: House_Prediction_Using_Two_File_Dataset.pdf Qualifier of the path: C: Is the path absolute? (True/False): True |
Again, we defined and initialized the $path
variable. Then, we used Split-Path cmdlet four times; every time with a different parameter to get the specified part of the $path
. These parameters include -Parent
, -Leaf
, -Qualifier
, and -IsAbsolute
. Using the -Parent
parameter, retrieved the parent folder of the House_Prediction_Using_Two_File_Dataset.pdf
file. It is the default parameter, which means we can get the parent folder even if we omit the -Parent
parameter.
The -Leaf
parameter provided us with the file name, while the -Qualifier
retrieved the root directory of the specified file. Finally, the -IsAbsolute
parameter returned a Boolean value, True
if the path is an absolute path; otherwise, False
. What is an absolute path? It is a complete file path from the root to the current file, such as C:\Users\John\Documents\file.txt.
For every parameter, we stored the returned value in a separate variable that is $pathParent
, $pathLeaf
, $pathQualifier
, and $pathIsAbsolute
that we enclosed within the array operator represented by @()
to make an array from them. Finally, we stored this array in the $pathIntoArray
variable.
While using the array operator (
@()
), we wrote a customized message to make every array element more understandable.
That’s split path into array in PowerShell.