One way in PowerShell to get a collection of file folders is by using the .NET System.IO.Directory.GetDirectories function. Time is short, and I am familiar with that, so I will use the .NET library in this example script.

Here is one way in which it can be done.

1$folders=[System.IO.Directory]::GetDirectories("c:\xxxx")
2
3foreach ($folderName in $folders)
4{                
5    ...
6}

If we wanted to, within the loop we could instantiate a System.IO.DirectoryInfo object to give us more information about the folder.

1$folder = New-Object System.IO.DirectoryInfo($folderName)                    

And then we can access the properties on that class, like the folder’s LastWriteTime in order to find out how old it is.

Here is a complete script.

 1$now = Get-Date
 2$yesterday = $now.AddDays(-1)
 3$folders=[System.IO.Directory]::GetDirectories("c:\program files")                    
 4
 5foreach ($folderName in $folders)                    
 6{
 7    $folder = New-Object System.IO.DirectoryInfo($folderName)
 8
 9    if ($folder.LastWriteTime -lt $yesterday)                    
10    {                     
11        Write-Host("Folder " + $folderName + " is at least a day old")                    
12    }
13    else
14    {
15        Write-Host("Folder " + $folderName + " is less than a day old")
16    }                    
17}
Please leave below any comments, feedback or suggestions, or alternatively contact me on a social network.

comments powered by Disqus