Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get-childitem sort-object : how to sort by CreationDate

I am trying to get the functionality of cmd.exe's "dir /o:d" in PowerShell.
I have tried:

(gci . | ? { $_.PSIsContainer } | sort CreationDate)    

d----         1/15/2013  11:46 AM   
d----          3/5/2013   4:54 PM  
d----         1/15/2013  12:31 PM  
d----         5/10/2013  10:08 AM  
d----         5/17/2013   2:21 PM  
d----         5/15/2013  10:09 AM  
d----         5/15/2013   6:00 PM  
d----         4/15/2013   8:10 AM  

Similarly,

(gci . | ? { $_.PSIsContainer } | sort CreationTime)      

d----         1/15/2013  11:46 AM    
d----         1/15/2013  12:31 PM   
d----          3/5/2013   4:54 PM  
d----         5/15/2013   6:00 PM      
d----         5/15/2013  10:09 AM     
d----         5/10/2013  10:08 AM     
d----         5/17/2013   2:21 PM   
d----         4/15/2013   8:10 AM  

What am I missing to make the directory list sort by creation date?
TIA for any insights...

like image 321
Number8 Avatar asked May 28 '13 13:05

Number8


People also ask

How do you sort an array of objects in PowerShell?

The default is ascending order. To sort multiple properties with different sort orders, use a hash table. For example, with a hash table you can sort one property in ascending order and another property in descending order. To sort objects, send them down the pipeline to Sort-Object .

How do I sort output in PowerShell?

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.

How do I sort a hash table in PowerShell?

To sort a hashtable, use the GetEnumerator() method on the hashtable to gain access to its individual elements. Then use the SortObject cmdlet to sort by Name or Value.

How do I get unique values in PowerShell?

The Get-Unique cmdlet compares each item in a sorted list to the next item, eliminates duplicates, and returns only one instance of each item. The list must be sorted for the cmdlet to work properly. Get-Unique is case-sensitive. As a result, strings that differ only in character casing are considered to be unique.

What is the difference between Get-ChildItem and Sort-Object?

The Get-ChildItem cmdlet gets the files from the directory specified by the Path parameter. The File parameter specifies that Get-ChildItem only gets file objects. The objects are sent down the pipeline to the Sort-Object cmdlet. Sort-Object uses the Length parameter to sort the files by length in ascending order.

How do I sort files by creation date in PowerShell?

Sorts files by creation date. Accepts parameter for path and file type. The SortFilesInFolderByCreationDate.ps1 Windows PowerShell script begins by creating three command-line parameters. This will allow you to change the directory location or the file type to sort without needing to actually edit the text of the script.

How does the Sort-Object cmdlet work?

The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties are not included in a command, PowerShell uses default sort properties of the first input object.

How do I use Get-ChildItem to find files?

Now, you should be able to use Get-ChildItem to find a variety of files in a variety of ways. You can use it to view the file system, the registry or any provider ( more info about providers here ). You can even take this a step further to delete the found files by piping them into Remove-Item.


1 Answers

dir /od sorts by modification time. If you want to do the same thing in powershell, you need to sort by the last write time, and not by creation time.

So that would be :

gci | sort lastwritetime
like image 168
jmd Avatar answered Oct 12 '22 13:10

jmd