• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Sort via creation time

M

Mark Line

#1
Hi,
I was wondering if anyone could help me,
I’m trying to get a folder name into a variable so I will be able to copy
the folder to another location.

I want the newest folder in a certain directory, the following commands
returns the correct information:

$newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select -last 1 |
select name

However $newestDir now contains “@{Name=FolderName}â€

I only want the name of the folder so I can append it to a path for copying
Any help would be fantastic?
Thanks
Mark
 

My Computer

J

Jacob Saaby Nielsen

#2
Hello Mark,

I modified your line a bit (sort is sort-object, and the current object in
the pipeline should be sorted on
its property creationtime), and it works now :)

PS C:\test> $newestDir = ls | ?{$_.psiscontainer} | sort-object $_.creationtime
| select -last 1 | select name
PS C:\test> $newestdir.name
2007-11-02-09

Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx

> Hi,
> I was wondering if anyone could help me,
> I’m trying to get a folder name into a variable so I will be able to
> copy
> the folder to another location.
> I want the newest folder in a certain directory, the following
> commands returns the correct information:
>
> $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
> -last 1 | select name
>
> However $newestDir now contains “@{Name=FolderName}â€
>
> I only want the name of the folder so I can append it to a path for
> copying
> Any help would be fantastic?
> Thanks
> Mark
 

My Computer

M

Mark Line

#3
Hi Jacob,

That works like a treat! thank you very much for your help :-)

Mark

"Jacob Saaby Nielsen" wrote:

> Hello Mark,
>
> I modified your line a bit (sort is sort-object, and the current object in
> the pipeline should be sorted on
> its property creationtime), and it works now :)
>
> PS C:\test> $newestDir = ls | ?{$_.psiscontainer} | sort-object $_.creationtime
> | select -last 1 | select name
> PS C:\test> $newestdir.name
> 2007-11-02-09
>
> Best Regards,
> Jacob Saaby Nielsen
> mailto:jacob.saaby@xxxxxx
>

> > Hi,
> > I was wondering if anyone could help me,
> > I’m trying to get a folder name into a variable so I will be able to
> > copy
> > the folder to another location.
> > I want the newest folder in a certain directory, the following
> > commands returns the correct information:
> >
> > $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
> > -last 1 | select name
> >
> > However $newestDir now contains “@{Name=FolderName}â€
> >
> > I only want the name of the folder so I can append it to a path for
> > copying
> > Any help would be fantastic?
> > Thanks
> > Mark
>
>
>
 

My Computer

S

Shay Levi

#4
Hi mark

You can get it in two ways

get-childitem | where {$_.psiscontainer} | sort creationtime -descending
| select name -first 1

Or

get-childitem | where {$_.psiscontainer} | sort creationtime | select name
-last 1



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



> Hi,
> I was wondering if anyone could help me,
> I’m trying to get a folder name into a variable so I will be able to
> copy
> the folder to another location.
> I want the newest folder in a certain directory, the following
> commands returns the correct information:
>
> $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
> -last 1 | select name
>
> However $newestDir now contains “@{Name=FolderName}”
>
> I only want the name of the folder so I can append it to a path for
> copying
> Any help would be fantastic?
> Thanks
> Mark
 

My Computer

J

Jacob Saaby Nielsen

#5
Goes to show how flexible PS really is. I know I'm still pretty impressed
with it ;o)

Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx

> Hi mark
>
> You can get it in two ways
>
> get-childitem | where {$_.psiscontainer} | sort creationtime
> -descending | select name -first 1
>
> Or
>
> get-childitem | where {$_.psiscontainer} | sort creationtime | select
> name -last 1
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

>> Hi,
>> I was wondering if anyone could help me,
>> I’m trying to get a folder name into a variable so I will be able to
>> copy
>> the folder to another location.
>> I want the newest folder in a certain directory, the following
>> commands returns the correct information:
>> $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
>> -last 1 | select name
>>
>> However $newestDir now contains “@{Name=FolderName}â€
>>
>> I only want the name of the folder so I can append it to a path for
>> copying
>> Any help would be fantastic?
>> Thanks
>> Mark
 

My Computer

K

Keith Hill [MVP]

#6
"Mark Line" <Mark Line@xxxxxx> wrote in message
news:07CECB51-D69C-4C53-8DA1-60EDDBAE3E07@xxxxxx

> Hi,
> I was wondering if anyone could help me,
> I’m trying to get a folder name into a variable so I will be able to copy
> the folder to another location.
>
> I want the newest folder in a certain directory, the following commands
> returns the correct information:
>
> $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select -last 1
> |
> select name
>
> However $newestDir now contains “@{Name=FolderName}â€
>
> I only want the name of the folder so I can append it to a path for
> copying
> Any help would be fantastic?
> Thanks
> Mark
>
Keep in mind that Select <propertyName> is like a SQL select I.e. it
projects the selected columns (properties in PowerShell) onto a new table
(PSCustomObject in PowerShell). That custom object has properties named the
same as the properties "selected" into that projection (like the column
names in SQL). If you really only want the path (unadorned) then try this:

$newestDir = ls | ?{$_.psiscontainer} | sort creationtime -desc |
select -first 1 | foreach{$_.Name}

--
Keith
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)