I want to get the name of the last modified folder. I have tried the below command but it is not giving me the correct folder name.
(Get-ChildItem c:\ -Directory).Name | Sort-object -Property lastWriteTime -Descending | Select -First 1
CodePudding user response:
Don't select the name in Get-ChildItem, but in the later select, and use -First because you are already sorting it descending:
Get-ChildItem c:\ -Directory | Sort-object -Property lastWriteTime -Descending | select name -first 1
CodePudding user response:
(Get-ChildItem -Path C:\example -Directory | Sort-Object LastWriteTime | Select-Object -Last 1).Name
Get-ChildItem -Path C:\example -Directory: gets a list of all the subfolders in the "C:\example" directory.
Sort-Object LastWriteTime: sorts the folders by their last modified date.
Select-Object -Last 1: selects the last folder in the sorted list.
.Name: displays the name of the selected folder.