Advertisement
Thunder-Menu

Driver$RecycleBin.ps1

Apr 10th, 2023
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 17.28 KB | Source Code | 0 0
  1. Add-Type -AssemblyName System.Web
  2. # Vérifier si l'exécution se fait en tant qu'administrateur
  3. if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  4.   Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
  5.   Exit
  6. }
  7.  
  8. # Chargement des assemblies nécessaires pour les contrôles Windows Forms
  9. Add-Type -AssemblyName System.Windows.Forms
  10. [System.Windows.Forms.Application]::EnableVisualStyles()
  11. Add-Type -AssemblyName System.Drawing
  12.  
  13.  
  14. [System.Windows.Forms.Application]::EnableVisualStyles()
  15. Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell -Name "MaxMemoryPerShellMB" -Value 2048
  16. Import-Module Microsoft.PowerShell.Management
  17.  
  18. # Définir les GUID des dossiers de la corbeille
  19.  
  20. $FOLDERID_RecycleBin = @(
  21.     [Guid]::Parse("B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC"), # Corbeille de l'utilisateur courant
  22.     [Guid]::Parse("D6D9E004-FD04-4AC4-8B08-6959FBEFF3D3"), # Corbeille de tous les utilisateurs
  23.     [Guid]::Parse("DE61D971-5EBC-4F02-A3A9-6C82895E5C04"), # Corbeille des fichiers système
  24.     [Guid]::Parse("6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD"), # Corbeille des documents
  25.     [Guid]::Parse("642167E6-5E41-48E2-AB3D-958981D7A7E4"), # Corbeille des fichiers en attente de suppression
  26.     [Guid]::Parse("E6FDF86B-F3D1-11D4-8576-0002A516ECE8"), # Corbeille des fichiers stockés sur un lecteur réseau
  27.     [Guid]::Parse("D3DCB472-7261-43CE-924B-ACCF719C32D9"), # Corbeille des fichiers stockés sur un lecteur réseau déconnecté
  28.     [Guid]::Parse("00BCFC5A-ED94-4e48-96A1-3F6217F21990"), # Corbeille des captures d'écran
  29.     [Guid]::Parse("6D809377-6AF0-444B-8957-A3773F02200E"), # Corbeille de la musique
  30.     [Guid]::Parse("FD228CB7-AE11-4AE3-864C-16F3910AB8FE"), # Corbeille des images
  31.     [Guid]::Parse("B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5"), # Corbeille des vidéos
  32.     [Guid]::Parse("9B174B35-40FF-11D2-A27E-00C04FC30871"), # Corbeille des fichiers Internet
  33.     [Guid]::Parse("724EF170-A42D-4FEF-9F26-B60E846FBA4F"), # Corbeille des fichiers temporaires
  34.     [Guid]::Parse("7B0DB17D-9CD2-4A93-9733-46CC89022E7C"),  # Corbeille des fichiers temporaires de l'utilisateur courant
  35.     [Guid]::Parse("0D4C3DB6-03A3-462F-A0E6-08924C41B5D4"), # Recycle Bin for the current user's pictures library.
  36.     [Guid]::Parse("2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39"), # Recycle Bin for the current user's music library.
  37.     [Guid]::Parse("F1B6C905-9A9C-47A9-802D-819EEFF47D67"), # Recycle Bin for the current user's videos library.
  38.     [Guid]::Parse("A5A9F9CF-357D-4FDE-A6FE-821E8E5876C3"), # GUID for the recycle bin on the selected drive
  39.     [Guid]::Parse("9E52AB10-F80D-49DF-ACB8-4330F5687855"), # Recycle bin for CDs/DVDs
  40.     [Guid]::Parse("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7"), # Recycle bin for external drives
  41.     [Guid]::Parse("E2FADA1C-69B3-4E7B-A4C1-172422B3B19F"), # Recycle bin for flash drives
  42.     [Guid]::Parse("DE4B071F-69AD-41E6-9A9D-3A8D1B3755FF") # Recycle bin for a specific user (where {SID} is the user's security identifier)
  43. )
  44.  
  45.  
  46. $dateComparisons = @(
  47.     @{Label="-lt (Get-Date)"; Value={"-lt (Get-Date)"}},
  48.     @{Label="-le (Get-Date)"; Value={"-le (Get-Date)"}},
  49.     @{Label="-eq (Get-Date)"; Value={"-eq (Get-Date)"}},
  50.     @{Label="-ge (Get-Date)"; Value={"-ge (Get-Date)"}},
  51.     @{Label="-gt (Get-Date)"; Value={"-gt (Get-Date)"}},
  52.     @{Label="-lt (Get-Date).AddDays(-7)"; Value={"-lt (Get-Date).AddDays(-7)"}},
  53.     @{Label="-lt (Get-Date).AddDays(-30)"; Value={"-lt (Get-Date).AddDays(-30)"}},
  54.     @{Label="-lt (Get-Date).AddDays(-365)"; Value={"-lt (Get-Date).AddDays(-365)"}},
  55.     @{Label="-lt (Get-Date).AddMonths(-1)"; Value={"-lt (Get-Date).AddMonths(-1)"}},
  56.     @{Label="-ge (Get-Date).AddYears(-11)"; Value={"-it (Get-Date).AddYears(-1)"}},
  57.     @{Label="-ge (Get-Date).AddYears(-11)"; Value={"-ge (Get-Date).AddYears(-11)"}})
  58.  
  59.  
  60. if ([Environment]::OSVersion.Version.Major -ge 6) {
  61.     $FOLDERID_RecycleBin += [Guid]::Parse("D574DE98-77DC-4b7f-9464-C394CFDD09DA") # Corbeille des applications
  62. }
  63.  
  64. if ([Environment]::OSVersion.Version.Major -ge 10) {
  65.     $FOLDERID_RecycleBin += [Guid]::Parse("0D4C3DB6-03A3-462F-A0E6-08924C41B5D4") # Corbeille des fichiers temporaires système
  66. }
  67.  
  68. # Importer la bibliothèque Windows API
  69. Add-Type -Namespace Win32 -Name "NativeMethods" -MemberDefinition @"
  70.    [DllImport("shell32.dll")]
  71.    public static extern int SHGetKnownFolderPath([In] ref Guid rfid, [In] uint dwFlags, [In] IntPtr hToken, [Out] out IntPtr pszPath);
  72. "@
  73.  
  74. # Importer la DLL nécessaire pour appeler la fonction SHGetKnownFolderPath
  75. Add-Type -AssemblyName System.Runtime.InteropServices
  76.  
  77. # Récupérer les chemins des dossiers de la corbeille en utilisant la méthode SHGetKnownFolderPath
  78. $recycleBinPaths = foreach ($guid in $FOLDERID_RecycleBin) {
  79.     $recycleBinPathPtr = [IntPtr]::Zero
  80.     [Win32.NativeMethods]::SHGetKnownFolderPath([ref]$guid, 0, [IntPtr]::Zero, [ref]$recycleBinPathPtr) | Out-Null
  81.     $recycleBinPath = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($recycleBinPathPtr)
  82.     [System.Runtime.InteropServices.Marshal]::FreeCoTaskMem($recycleBinPathPtr) | Out-Null
  83.     $recycleBinPath
  84. }
  85.  
  86. Add-Type -AssemblyName System.Windows.Forms
  87.  
  88. $form = New-Object System.Windows.Forms.Form
  89. $form.Text = "Select a Drive and Command"
  90. $form.ClientSize = New-Object System.Drawing.Size(400, 100)
  91. $form.StartPosition = "CenterScreen"
  92.  
  93. $driveLabel = New-Object System.Windows.Forms.Label
  94. $driveLabel.Location = New-Object System.Drawing.Point(10, 10)
  95. $driveLabel.Size = New-Object System.Drawing.Size(80, 20)
  96. $driveLabel.Text = "Select a drive:"
  97. $form.Controls.Add($driveLabel)
  98.  
  99. $driveComboBox = New-Object System.Windows.Forms.ComboBox
  100. $driveComboBox.Location = New-Object System.Drawing.Point(100, 10)
  101. $driveComboBox.Size = New-Object System.Drawing.Size(200, 25)
  102.  
  103. $driveComboBoxx = New-Object System.Windows.Forms.ComboBox
  104. $driveComboBoxx.Location = New-Object System.Drawing.Point(100, 10)
  105. $driveComboBoxx.Size = New-Object System.Drawing.Size(200, 25)
  106.  
  107. # Get local and removable disks
  108. $disks = Get-CimInstance Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 -or $_.DriveType -eq 2 }
  109.  
  110. # Add each disk to the combo box
  111. foreach ($disk in $disks) {
  112.     $driveComboBox.Items.Add($disk.DeviceID)
  113. }
  114.  
  115. $form.Controls.Add($driveComboBox)
  116. $commandLabel = New-Object System.Windows.Forms.Label
  117. $commandLabel.Location = New-Object System.Drawing.Point(10, 40)
  118. $commandLabel.Size = New-Object System.Drawing.Size(90, 20)
  119. $commandLabel.Text = "Select a command:"
  120. $form.Controls.Add($commandLabel)
  121. $commandComboBox = New-Object System.Windows.Forms.ComboBox
  122. $commandComboBox.Location = New-Object System.Drawing.Point(100, 40)
  123. $commandComboBox.Size = New-Object System.Drawing.Size(200, 25)
  124. $commandComboBox.Items.Add("All Files")
  125. $commandComboBox.Items.Add("All Directories")
  126. $commandComboBox.Items.Add("All Directories and Files")
  127. $form.Controls.Add($commandComboBox)
  128.  
  129. # $commandLabel = New-Object System.Windows.Forms.Label
  130. # $commandLabel.Location = New-Object System.Drawing.Point(10, 40)
  131. # $commandLabel.Size = New-Object System.Drawing.Size(90, 20)
  132. # $commandLabel.Text = "Select mod:"
  133. # $form.Controls.Add($commandLabel)
  134. # $ComboBox = New-Object System.Windows.Forms.ComboBox
  135. # $ComboBox.Location = New-Object System.Drawing.Point(10, 60)
  136. # $ComboBox.Size = New-Object System.Drawing.Size(200, 30)
  137. # $dateComparisons | ForEach-Object {
  138. #     $ComboBox.Items.Add($_.Label) | Out-Null
  139. # }
  140. # $ComboBox.SelectedIndex = 0
  141. # $form.Controls.Add($ComboBox)
  142.  
  143. # Add an OK button to close the window
  144. $okButton = New-Object System.Windows.Forms.Button
  145. $okButton.Location = New-Object System.Drawing.Point(310, 10)
  146. $okButton.Size = New-Object System.Drawing.Size(70, 55)
  147. $okButton.Text = "OK"
  148. $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  149. $form.AcceptButton = $okButton
  150. $form.Controls.Add($okButton)
  151. $selectedDrive = @()
  152.  
  153.  
  154. function Show-DeletedFiles {
  155.     param (
  156.         [Parameter(Mandatory=$true, Position=0)]
  157.         [System.Collections.ArrayList]$DeletedFiles
  158.     )
  159.  
  160.     $table = @()
  161.     foreach ($file in $DeletedFiles) {
  162.         $row = [PSCustomObject]@{
  163.             Name = $file.Name
  164.             Type = $file.Extension
  165.             OriginalLocation = $file.DirectoryName
  166.             Size = $file.Length
  167.             DeletedDate = $file.LastWriteTime
  168.             IsFolder = $file.Attributes -eq 'Directory'
  169.         }
  170.         $table += $row
  171.     }
  172.  
  173.     $table | Format-Table -AutoSize
  174. }
  175.  
  176. #selectedComparison = @()
  177. $deletedFiles = @()
  178. $excludedFolders = @("FileCache", "ModuleCache") # Add the names of any folders to exclude from the search
  179. if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
  180.     # Get the selected drive
  181.     $selectedDrive = $driveComboBox.SelectedItem.ToString()
  182.     Write-Host "The selected drive is $selectedDrive"
  183.    
  184. $recycleBinPath = $null
  185. $recycleBinPath = Join-Path -Path $selectedDrive -ChildPath $RecycleBin
  186. #$selectedComparison = $dateComparisons[$comparisonComboBox.SelectedIndex].Value
  187. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -eq 'Directory' -and $_.Name -match $Recycle.Bin }
  188. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  189. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Recurse -Force -ErrorAction SilentlyContinue -Include *.* -File -Exclude $excludedFolders
  190. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  191. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force -Recurse | Where-Object { $_.Name -match $RecycleBin }
  192. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.Name -match $RecycleBin }
  193. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  194. #$recycleBinFolders += Get-ChildItem -Path $recycleBinPath -Force -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -ne 'Directory' -and $_.Name -match $RecycleBin }
  195. #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  196. $recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  197.  
  198. #| Where-Object { $_.Name -match $Recycle.Bin }
  199. # Check if the Recycle Bin path exists
  200. if ($recycleBinFolders.Count -gt 0 -and (Test-Path $recycleBinFolders[0].FullName)) {
  201.     # Get all deleted files or directories in the Recycle Bin, including hidden items
  202.     if ($commandComboBox.SelectedIndex -eq 0) {
  203.         $deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Force -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.PSIsContainer -eq $false }
  204.     } elseif ($commandComboBox.SelectedIndex -eq 1) {
  205.         $deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Force -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.PSIsContainer -eq $true }
  206.     } elseif ($commandComboBox.SelectedIndex -eq 2) {
  207.       $recycleBinPath = Join-Path -Path $selectedDrive -ChildPath $RecycleBin
  208. #$deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  209. #$deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -eq 'Directory' -and $_.Name -match $Recycle.Bin }
  210. #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) } #246331
  211. #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) } #252687
  212. #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) } #242601
  213. #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -File -Force | Where-Object {$_.LastWriteTime -ge (Get-Date).AddYears(-11)} #245155
  214. #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -File -Force | Where-Object {$_.LastWriteTime -ge (Get-Date).AddYears(-11)} #221759
  215. #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force -Attributes Directory, Archive | Where-Object { $_.LastWriteTime -lt (Get-Date) } #212263
  216. #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) } #246341
  217. #$recycleBinPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop), $RecycleBin)
  218. # $deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) }
  219. $deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
  220.     }
  221.  
  222.         if ($deletedItems.Count -gt 0) {
  223.             Write-Host "$($deletedItems.Count) deleted items found in the Recycle Bin."
  224.         } else {
  225.             Write-Host "No deleted items found in the Recycle Bin."
  226.         }
  227.     } elseif ($commandComboBox.SelectedIndex -eq 1) {
  228.         $Shell = New-Object -ComObject Shell.Application
  229.         $recycleBinFolder = $Shell.Namespace(0xa)
  230.         $recycleBinItems = $recycleBinFolder.Items()
  231.         $deletedItems = @()
  232.  
  233.         foreach ($item in $recycleBinItems) {
  234.             if ($commandComboBox.SelectedIndex -eq 0 -and $item.IsFolder -eq $false) {
  235.                 $deletedItem = [PSCustomObject]@{
  236.                     Name = $item.Name
  237.                     Type = $recycleBinFolder.GetDetailsOf($item, 0)
  238.                     OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
  239.                     Size = $recycleBinFolder.GetDetailsOf($item, 2)
  240.                     DeletedDate = ($recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder')
  241.                   IsFolder = ($recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder')
  242.                 }
  243.                 $deletedItems += $deletedItem
  244.             } elseif ($commandComboBox.SelectedIndex -eq 1 -and $item.IsFolder -ne $true) {
  245.                 $deletedItem = [PSCustomObject]@{
  246.                     Name = $item.Name
  247.                     Type = $recycleBinFolder.GetDetailsOf($item, 0)
  248.                     OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
  249.                     Size = $recycleBinFolder.GetDetailsOf($item, 2)
  250.                     DeletedDate = ($recycleBinFolder.GetDetailsOf($item, 4) -ne 'Folder')
  251.                       IsFolder = ($recycleBinFolder.GetDetailsOf($item, 4) -ne 'Folder')
  252.                 }
  253.                 $deletedItems += $deletedItem
  254.             }
  255. elseif ($commandComboBox.SelectedIndex -eq 2) {
  256.                 $deletedItem = [PSCustomObject]@{
  257.                     Name = $item.Name
  258.                     Type = $recycleBinFolder.GetDetailsOf($item, 0)
  259.                     OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
  260.                     Size = $recycleBinFolder.GetDetailsOf($item, 2)
  261.     DeletedDate = $recycleBinFolder.GetDetailsOf($item, 4)
  262.         IsFolder = $recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder'
  263.                 }
  264.                 $deletedItems += $deletedItem
  265.             }
  266.       }
  267.     }  
  268.     # Show the deleted files in a table
  269.         if ($deletedItems.Count -gt 0) {
  270.             # Append the new items to the existing array
  271.             $deletedFiles += $deletedItems
  272.         }
  273.         if ($deletedFiles.Count -gt 0) {
  274.             Show-DeletedFiles $deletedFiles
  275.         } else {
  276.             Write-Host "No items found in the Recycle Bin."
  277.         }
  278. }
  279.  
  280. if ($deletedFiles -ne $null) {
  281. $deletedItems = $deletedFiles
  282. }
  283.  
  284. # Définir le filtre de fichier pour le SaveFileDialog
  285. $saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
  286. $saveFileDialog.Filter = "Tous les fichiers (*.*)|*.*"
  287. $saveFileDialog.Title = "Sélectionner un emplacement de restauration"
  288.  
  289. # Si l'utilisateur sélectionne un fichier à restaurer, ouvrir le SaveFileDialog pour sélectionner un emplacement de restauration
  290. if ($deletedItems.Count -gt 0) {
  291.     $deletedItem = $deletedItems | Out-GridView -Title "Sélectionner un fichier à restaurer" -PassThru
  292.     if ($deletedItem) {
  293.         $fileToRestore = $deletedItem.Name
  294.         $saveFileDialog.FileName = $fileToRestore
  295.         $saveFileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($fileToRestore)
  296.         $saveFileDialog.DefaultExt = [System.IO.Path]::GetExtension($fileToRestore)
  297.         $saveFileDialog.CheckPathExists = $true
  298.  
  299.         if ($saveFileDialog.ShowDialog() -eq 'OK') {
  300.             $restorePath = $saveFileDialog.FileName
  301.             Copy-Item -Path $deletedItem.FullName -Destination $restorePath
  302.             Write-Output "Le fichier $fileToRestore a été restauré avec succès à l'emplacement $restorePath."
  303.         }
  304.     }
  305. }
  306. else {
  307.     Write-Output "Aucun fichier supprimé n'a été trouvé dans la corbeille."
  308. }
  309. Pause
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement