# Set the path to the directory containing the folders $directoryPath = "$env:APPDATA\Mozilla\Firefox\Profiles" # Get all folders in the directory $folders = Get-ChildItem -Path $directoryPath -Directory # Sort the folders by their last write time (modification date) in descending order $latestFolders = $folders | Sort-Object LastWriteTime -Descending | Select-Object -First 2 # Calculate the date 7 days ago $sevenDaysAgo = (Get-Date).AddDays(-7) # Loop through all folders foreach ($folder in $folders) { # Check if the folder is not in the list of latest folders if ($folder.FullName -notin $latestFolders.FullName) { # If the folder has "_old" suffix if ($folder.Name -like "*_old") { # Check if the folder was modified more than 7 days ago if ($folder.LastWriteTime -lt $sevenDaysAgo) { # Remove the folder Remove-Item -Path $folder.FullName -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue | Out-Null } } else { # Rename the folder by adding "_old" suffix $newName = "$($folder.Name)_old" Rename-Item -Path $folder.FullName -NewName $newName -Force -ErrorAction SilentlyContinue } } }