# 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 # Loop through all folders and rename the ones that are not the latest foreach ($folder in $folders) { if ($folder.FullName -notin $latestFolders.FullName) { $newName = "$($folder.Name)_old" Rename-Item -Path $folder.FullName -NewName $newName -Force -ErrorAction SilentlyContinue } }