windows - Powershell: How to output folder name, lastwritetime and folder size in one line?

Windows - Powershell: How to output folder name, lastwritetime and folder size in one line?

You can use PowerShell to achieve this by using the Get-ChildItem cmdlet to retrieve information about folders, and then select the properties you want to display. You can then format the output to display the information in a single line.

Here's how you can do it:

Get-ChildItem -Directory | ForEach-Object {
    $_.Name + " | " + $_.LastWriteTime + " | " + "{0:N2} MB" -f ((Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB)
}

Let's break down what this script does:

  • Get-ChildItem -Directory: This cmdlet retrieves all the directories (folders) in the current directory.
  • ForEach-Object: This cmdlet allows you to perform an operation on each directory returned by Get-ChildItem.
  • $_: Represents the current directory being processed in the loop.
  • Name: Retrieves the name of the directory.
  • LastWriteTime: Retrieves the last write time of the directory.
  • "{0:N2} MB" -f ((Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB): This part calculates the total size of the directory (including subdirectories) in megabytes. It uses the Get-ChildItem cmdlet with the -Recurse parameter to get all files in the directory and its subdirectories, then pipes them to the Measure-Object cmdlet to calculate the sum of their lengths. Finally, it divides the sum by 1MB to convert it to megabytes and formats the output to display two decimal places.

This script will output each folder's name, last write time, and size in megabytes in a single line for each folder. You can adjust the formatting or add additional properties as needed.

Examples

  1. Powershell: List folders with name, last write time, and size

    • Description: This query aims to list folders in PowerShell with their name, last write time, and size in a single line output.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  2. Powershell: Show folder details - name, last modified date, size

    • Description: This query focuses on displaying details of folders including name, last modified date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name "Size" -Value (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum; $_ } | Select-Object Name, LastWriteTime, Size | Format-Table -AutoSize
    
  3. Powershell: Display folder properties - name, last modified, size

    • Description: This query aims to display folder properties such as name, last modified date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  4. Powershell: List directory names, last write date, and size

    • Description: This query lists directory names, their last write date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  5. Powershell: Get folders with name, last write time, and size

    • Description: This query retrieves folders along with their name, last write time, and size using PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  6. Powershell: Show folder info - name, last modified, size

    • Description: This query displays folder information including name, last modified date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  7. Powershell: List directories with name, last write time, and size

    • Description: This query lists directories along with their name, last write time, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  8. Powershell: View folder details - name, last modified, size

    • Description: This query provides a view of folder details such as name, last modified date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  9. Powershell: Display folders with name, last write time, and size

    • Description: This query displays folders along with their name, last write time, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    
  10. Powershell: List folder properties - name, last modified, size

    • Description: This query lists folder properties including name, last modified date, and size in PowerShell.
    Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name, LastWriteTime, @{Name="Size";Expression={(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum}} | Format-Table -AutoSize
    

More Tags

label pytorch chmod angular-ngfor google-sheets-macros type-conversion core-animation vsto azure-configuration react-functional-component

More Programming Questions

More Electronics Circuits Calculators

More Retirement Calculators

More Date and Time Calculators

More Dog Calculators