Get last element of pipeline in powershell Get last element of pipeline in powershell powershell powershell

Get last element of pipeline in powershell


A few ways to do this:

$name = 'c:\temp\aaa.bbb.ccc'# way 1$name.Split('.') | Select-Object -Last 1# way 2[System.IO.Path]::GetExtension($name)# or if the dot is not needed[System.IO.Path]::GetExtension($name).TrimStart('.')


In general, getting the last element in the pipeline would be done using Select -Last 1 as Roman suggests above. However, an alternate and easier way to do this if the input is a simple array is to use array slicing e.g.:

PS> $name = "c:\temp\aaa.bbb.txt"PS> $name.Split('.')[-1]txt

Was your intent to get the file's extension or basename? Because it seems that the Type property already contains the extension for the filename.