Powershell query - list all old files in set of folders

Nancy J 41 Reputation points
2022-04-07T14:03:50.39+00:00

I need to build a script that does the following:

1 - Checks if a network drive is mapped, if it is not then it maps it.
2 - Lists all folders in the root of a network drive
3 - For each folder in that list, it exports a list of all files older than 7 years to a CSV
4 - Renames the CSV files

I put together something that works when I specify a folder in part 3 but if I try to make it work with a foreach it just hangs.

How do I modify this so I get 1 CSV file for each root folder on the network drive? Lines 27-31 are not commented out on my side, not sure why they show as commented when I paste the code here.

This is the code:

#Variables
$testmapdrive = "S:\test" 
$folderpath = "S:\"+$Name
$csvpath = "S:\IT Records\Data Lifecycle Reviews\2022-04"
$csvfilename = $csvpath + "\" + $Name + ".csv"
$minage = (get-date).AddYears(-7)

#Check if S:\ mapped, if not it maps the drive

If (Test-Path -Path $testmapdrive) {
    Write-Host "Drive Exists already"
}
Else {
    #map network drive
    (New-Object -ComObject WScript.Network).MapNetworkDrive("s:","\\dfspath\dfs")

    #check mapping again
    If (Test-Path -Path $testmapdrive) {
        Write-Host "Drive has been mapped"
    }
    Else {
        Write-Host "Something went very wrong"
    }
}

#Get folder names and generate lists of files in each older than 7 years
$folder = Get-ChildItem -Path "S:\" -Depth 0 | Select Name
foreach ($Name in $folder)
{
Get-ChildItem -Path $folderpath -File -Recurse -Force -ErrorAction SilentlyContinue | ?{$_.lastwritetime -lt $minage} | Select Directory,Name,CreationTime,LastWriteTime| Export-csv $csvfilename -NoTypeInformation
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,388 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 97,486 Reputation points MVP
    2022-04-07T21:22:24.96+00:00

    Hi @Nancy J ,

    the issue why your script is not working ist this:

    191123-image.png

    This script below works here. I just modified the path and removed the | Select Name in line 6. Also make sure the folders records and 2022-04 exists in file system.

    #Variables   
    $rootfolder = "C:\Junk"  
    $csvpath = "C:\Junk\records\2022-04"  
    #Get folder names and generate lists of files in each  
    $folder = Get-ChildItem -Path $rootfolder -Depth 0 -Directory  
    foreach ($Name in $folder) {  
      $Name  
      $folderpath = "$rootfolder\$Name"  
      $csvfilename = "$csvpath\$Name.csv"  
      Get-ChildItem -Path $folderpath -Recurse -Force -ErrorAction SilentlyContinue |  
       Select-Object Directory, Name, CreationTime, LastWriteTime |  
        Export-Csv $csvfilename -NoTypeInformation  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


5 additional answers

Sort by: Most helpful
  1. Nancy J 41 Reputation points
    2022-04-08T11:36:43.773+00:00

    That was it, thank you so much! This script is going to save us days of work and I can't thank you enough.

    0 comments No comments