1

I am going through folders we have on a work drive that have files over 20 years old in them. We want to move these files off this drive to clear up room and organize it better.

I have a PowerShell script to look through the folders and output to a csv file.

It puts into the csv file but there are thousands of files here. We only want to have the file with the latest modified on there. It is giving me every file.

Can I have it only output the latest modified file from that folder and then move onto the next folder?

Get-ChildItem -LiteralPath Y:\XYZ\MainFolder -Recurse -Depth 2  |Format-Table| Out-File C:\Users\USER\Desktop\aCSVfile.csv
Moerwald
  • 10,448
  • 9
  • 43
  • 83
hamstar
  • 11
  • 1
  • 2
  • You should never pipe `Format-Table` to anything. You want just 1 file? or 1 file per folder? – TheMadTechnician Jun 26 '19 at 01:05
  • besides the `Format-*` mistake that TheMadTechnician pointed out, do you want the newest file in the "MainFolder" or for the immediate dirs beneath it, or for every folder in the entire tree? – Lee_Dailey Jun 26 '19 at 01:28
  • Are you looking for export the folder list which is older then X date ? – Nirav Mistry Jun 26 '19 at 04:10
  • Thanks for the response. I'm very new to using powershell so I know fairly nothing. Why shouldn't I pipe [tag:Format-Table]? I would like the last modified for each folder and then go onto the next folder. I want to go the 2 directories beneath the main one, I went down just the initial folder and it wasn't deep enough. I want to put the file into an excel file and sort the files by 1-3 years, 4-6 years, 6-9 years, and 10+. – hamstar Jun 27 '19 at 00:33

1 Answers1

3

You'll need PowerShell 5.x otherwise, the -Directory switch of Get-ChildItem will not be available:

Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } | Export-Csv "test.csv" -NoTypeInformation -Delimiter ";"
  • Get-ChildItem -Path . -Directory -Recurse - Get all directories
  • Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } - check if the last write time is older than 2018-10-04 (you may also use LastAccessTime)
  • Export-Csv "test.csv" -NoTypeInformation -Delimiter ";" - exports the filtered objects to CSV file.

If you want less information in the CSV file you can use Select-Object:

Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } |Select-Object Name, PSPath, Parent | Export-Csv "test.csv" -NoTypeInformation -Delimiter ";"          

To get available properties use Get-Member:

 Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } | Get-Member

Output:

    TypeName: System.IO.DirectoryInfo

    Name                      MemberType     Definition
    ----                      ----------     ----------
    LinkType                  CodeProperty   System.String LinkType{get=GetLinkType;}
    Mode                      CodeProperty   System.String Mode{get=Mode;}
    ... 
    FullName                  Property       string FullName {get;}
    LastAccessTime            Property       datetime LastAccessTime {get;set;}
    LastAccessTimeUtc         Property       datetime LastAccessTimeUtc {get;set;}
    LastWriteTime             Property       datetime LastWriteTime {get;set;}
    LastWriteTimeUtc          Property       datetime LastWriteTimeUtc {get;set;}
    Name                      Property       string Name {get;}
    ...
    Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
    CreationTime              Property       datetime CreationTime {get;set;}
    CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}
    ...

From here you can use the *Property member types in Select-Object and Where-Object.

If you are on PowerShell less than 5, you've to check the PSIsContainer property, as explained in this stackoverflow answer.

Hope that's the information you're looking for.

Moerwald
  • 10,448
  • 9
  • 43
  • 83