Get last element of pipeline in powershell

Posted by dozacinc on Stack Overflow See other posts from Stack Overflow or by dozacinc
Published on 2010-12-28T13:55:32Z Indexed on 2010/12/28 16:53 UTC
Read the original article Hit count: 231

Filed under:
|

Hi,

This might be weird, but stay with me. I want to get only the last element of a piped result to be assigned to a varaiable. I know how I would do this in "regular" code of course, but since this must be a one-liner.

More specifically, I'm interested in getting the file extension when getting the result from an FTP request ListDirectoryDetails. Since this is done within a string expansion, I can't figure out the proper code. Currently I'm getting the last 3 hars, but that is real nasty.

New-Object PSObject -Property @{
LastWriteTime = [DateTime]::ParseExact($tempDate, "MMM dd HH:mm",[System.Globalization.CultureInfo]::InvariantCulture)
Type = $(if([int]$tempSize -eq 0) { "Directory" } else { $tempName.SubString($tempName.length-3,3) })
Name = $tempName
Size = [int]$tempSize
}

My idea was doing something similar to
$tempName.Split(".") | ? {$_ -eq $input[$input.Length-1]}
that is, iterate over all, but only take out where the element I'm looking at is the last one of the input-array.

What am I missing ? Fire at will
/M

© Stack Overflow or respective owner

Related posts about powershell

Related posts about pipeline