Table of Contents
Using Split()
Method
Use the Split()
method with the indexing operator to split the specified string and get the last value in PowerShell.
1 2 3 4 5 |
$string = "This is a sample string" $lastElement = ($string.Split(" "))[-1] Write-Host "The last element of '$string' is '$lastElement'" |
1 2 3 |
The last element of 'This is a sample string' is 'string' |
First, we defined a $string
variable and initialized it with a string value, as shown in the above code fence. Next, we used the Split()
method, passed whitespace as a delimiter, and chained this method with $string
to split it wherever it found a whitespace.
We enclosed the output returned by the Split()
method in parentheses and used the indexing operator ([]
) to get the last value. In PowerShell, we can use -1
to get the last index or element from an array. Finally, we used the Write-Host
cmdlet to print the customized output showing the complete string and its last element wrapped with single quotes.
Use the Split()
method with the Select-Object
cmdlet to split the given string and get the last value in PowerShell.
1 2 3 4 5 |
$string = "This is a sample string" $lastElement = ($string.Split(" ")) | Select-Object -Last 1 Write-Host "The last element of '$string' is '$lastElement'" |
1 2 3 |
The last element of 'This is a sample string' is 'string' |
This code is similar to the previous one, but we piped the output of the Split()
method to the Select-Object
cmdlet.
This cmdlet is used to choose specified properties of the object or a set of objects. We can also use this cmdlet to select the given number of objects, unique objects, or objects in a particular position in the specified array. Additionally, we can use -First
, -Last
, -Skip
, -Unique
, and -Index
parameters to select objects from a collection such as an array.
We used the Select-Object
cmdlet with the -Last
parameter whose value was set to 1
to get the last element of the $string
. Finally, we used the Write-Host
cmdlet to display the customized results on the PowerShell console.
We can also specify the
2
,3
, …, orn
number of words preceded by the-Last
parameter to get them as one string element.
Using -Split
Operator
Use the -Split
operator with the indexing operator to split the specified string and get the last value in PowerShell.
1 2 3 4 5 |
$string = "This is a sample string" $lastElement = ($string -Split " ")[-1] Write-Host "The last element of '$string' is '$lastElement'" |
1 2 3 |
The last element of 'This is a sample string' is 'string' |
Use the -Split
operator with the Select-Object
cmdlet to split the specified string and get the last value in PowerShell.
1 2 3 4 5 |
$string = "This is a sample string" $lastElement = ($string -Split " ") | Select-Object -Last 1 Write-Host "The last element of '$string' is '$lastElement'" |
1 2 3 |
The last element of 'This is a sample string' is 'string' |
Above, both code snippets are the same as the examples in the previous section where we used the Split()
method. The only difference is that we used the -Split
operator this time, which did the same as the Split()
method, but there are some differences and similarities that you can find [here](Please add ‘PowerShell Split Path into Array’ article’s link, it is from MS11).
Until now, we split a simple string and retrieved the last element from it; suppose we have a path as a string value and are told to retrieve the last value from it. Here, the last value can be a file or folder name. See the following section to explore it.
Using System.IO.Path
Class
To split the specified path & directory and get the last value in PowerShell:
- Store the path in a variable.
- Use the
GetDirectoryName()
method to retrieve the directory from the path and store it in a variable. - Use the
Split()
method with the indexing operator twice to get the last element from the path and directory created in the first and second steps. - Use the
Write-Host
cmdlet to display customized results on the PowerShell console.
1 2 3 4 5 6 7 8 9 10 |
$path = "C:\Users\Mehvish\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $directory = [System.IO.Path]::GetDirectoryName($path) $pathLastElement = ($path.Split("\"))[-1] $directoryLastElement = ($directory.Split("\"))[-1] Write-Host "Path: $path" Write-Host "Last element of the path:'$pathLastElement'" Write-Host "Directory: $directory" Write-Host "Last element of the directory:'$directoryLastElement'" |
1 2 3 4 5 6 |
Path: C:\Users\Mehvish\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf Last element of the path:'House_Prediction_Using_Two_File_Dataset.pdf' Directory: C:\Users\Mehvish\Desktop\Samples Last element of the directory:'Samples' |
After storing the path in the $path
variable, we used the GetDirectoryName()
method of the Path
class. First, we passed the $path
as a parameter to get its directory, which we stored in the $directory
variable. Next, we chained the Split()
method with $path
and $directory
variables separately and passed \
as a delimiter to split them based on the \
character; however, the indexing operator with -1
value was used to get the last value for both of them. Finally, we used the Write-Host
cmdlet to print the customized messages to make the output easier to understand.
Use the System.IO.Path
class with the Select-Object
cmdlet to split the specified path & directory and get the last value in PowerShell.
1 2 3 4 5 6 7 8 9 10 |
$path = "C:\Users\Mehvish\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf" $directory = [System.IO.Path]::GetDirectoryName($path) $pathLastElement = ($path.Split("\")) | Select-Object -Last 1 $directoryLastElement = ($directory.Split("\"))[-1] | Select-Object -Last 1 Write-Host "Path: $path" Write-Host "Last element of the path:'$pathLastElement'" Write-Host "Directory: $directory" Write-Host "Last element of the directory:'$directoryLastElement'" |
1 2 3 4 5 6 |
Path: C:\Users\Mehvish\Desktop\Samples\House_Prediction_Using_Two_File_Dataset.pdf Last element of the path:'House_Prediction_Using_Two_File_Dataset.pdf' Directory: C:\Users\Mehvish\Desktop\Samples Last element of the directory:'Samples' |
This example is similar to the previous one, but we used the Select-Object
with the -Last
parameter to get the last value. Note that we have already learned the [Select-Object while using Split() method](add link of the first section here).
Until now, we used different approaches to split the simple string, but what if we need to split a particular string which is the value of an object’s property? Don’t worry; let’s dive into the following section to learn it.
Using Split()
, -Split
for an Object’s Property
Use the Split()
method and the -Split
operator with the indexing operator to split the value of the object’s property and get its last element in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$studentObject = New-Object PSObject -Property @{ studentFirstName = "Mehvish" studentLastName = "Ashiq" studentAge = 30 Address = "123 Main St, sometown, UK" } $lastElementUsingSplitOperator = ($studentObject.Address -Split ' ')[-1] $lastElementUsingSplitFunction = ($studentObject.Address.Split(' '))[-1] Write-Host "Last element using -Split operator with indexing operator: '$lastElementUsingSplitOperator'" Write-Host "Last element using Split() method with indexing operator: '$lastElementUsingSplitFunction'" |
1 2 3 4 |
Last element using -Split operator with indexing operator: 'UK' Last element using Split() method with indexing operator: 'UK' |
For the above example, we used the New-Object
cmdlet to create an object and store it in the $studentObject
variable. The -Property
parameter was used to set a new object’s property values and call methods. This object had key=value
pairs; each pair was separated by a new line (line break) where key
was the property name (such as $studentAge
) and value was the property value (such as 30
).
The
key
can also be methods, and thevalue
can be method arguments.
We derived our new object from the PSOObject
class because it helps to specify those properties that don’t exist on an object. So, the New-Object
cmdlet specified properties to the object as NoteProperty
.
After creating the object, we used dot notation (.
) as $studentObject.Address
to retrieve the value of the Address
property. Then, we used the -Split
operator with an indexing operator to get the last value that we stored in the $lastElementUsingSplitOperator
variable. Similarly, we retrieved the last element using the Split()
method and stored the value in the $lastElementUsingSplitFunction
variable. Finally, we used the Write-Host
cmdlet to display customized results on the console.
Use the Split()
method and the -Split
operator with the Select-Object
cmdlet to split the value of the object’s property and get its last element in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$studentObject = New-Object PSObject -Property @{ studentFirstName = "Mehvish" studentLastName = "Ashiq" studentAge = 30 Address = "123 Main St, sometown, UK" } $lastElementUsingSplitOperator = ($studentObject.Address -Split ' ') | Select-Object -Last 1 $lastElementUsingSplitFunction = ($studentObject.Address.Split(' ')) | Select-Object -Last 1 Write-Host "Last element using -Split operator with Select-Object cmdlet: '$lastElementUsingSplitOperator'" Write-Host "Last element using Split() method with Select-Object cmdlet: '$lastElementUsingSplitFunction'" |
1 2 3 4 |
Last element using -Split operator with Select-Object cmdlet: 'UK' Last element using Split() method with Select-Object cmdlet: 'UK' |
This example is the same as the last one, but we used the Select-Object
cmdlet with the -Last
parameter to get the last element.
That’s all about PowerShell split and get last value.