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...
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 .
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.
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With