问题
I have the following powershell command
get-childitem $FilePath | select {$_.Fullname}
This will output the name of ALL subfolders in $FilePath. How do I output the subfolder with the latest write time?
回答1:
I believe this is what you are after:
Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | Select {$_.FullName} -First 1
If you want to see the last write time too, you can use this:
Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | Select {$_.FullName, $_.LastWriteTime} -First 1
This will look at the Last Write Time of both files and folders in the given $FilePath. If you are after just files then provide the -File switch to Get-ChildItem, and if you are only interested in folders then provide the -Directory switch. Also, if you want to know the Last Write Time of any files/folders in the $FilePath including in sub-folders, then provide the -Recurse switch to Get-ChildItem.
If you want to see more than just the 1 file, change -First 1 to the number of files that you want to see.
Also, if you want to instead see the file with the oldest Last Write Time, just remove the -Descending switch parameter to the Sort command.
回答2:
Sort the items and select the last one (default sort order is ascending):
Get-ChildItem $FilePath | sort LastWriteTime | select -Last 1 FullName
来源:https://stackoverflow.com/questions/19458186/output-sub-folder-with-the-latest-write-access