Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Web
- # Vérifier si l'exécution se fait en tant qu'administrateur
- if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
- Exit
- }
- # Chargement des assemblies nécessaires pour les contrôles Windows Forms
- Add-Type -AssemblyName System.Windows.Forms
- [System.Windows.Forms.Application]::EnableVisualStyles()
- Add-Type -AssemblyName System.Drawing
- [System.Windows.Forms.Application]::EnableVisualStyles()
- Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell -Name "MaxMemoryPerShellMB" -Value 2048
- Import-Module Microsoft.PowerShell.Management
- # Définir les GUID des dossiers de la corbeille
- $FOLDERID_RecycleBin = @(
- [Guid]::Parse("B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC"), # Corbeille de l'utilisateur courant
- [Guid]::Parse("D6D9E004-FD04-4AC4-8B08-6959FBEFF3D3"), # Corbeille de tous les utilisateurs
- [Guid]::Parse("DE61D971-5EBC-4F02-A3A9-6C82895E5C04"), # Corbeille des fichiers système
- [Guid]::Parse("6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD"), # Corbeille des documents
- [Guid]::Parse("642167E6-5E41-48E2-AB3D-958981D7A7E4"), # Corbeille des fichiers en attente de suppression
- [Guid]::Parse("E6FDF86B-F3D1-11D4-8576-0002A516ECE8"), # Corbeille des fichiers stockés sur un lecteur réseau
- [Guid]::Parse("D3DCB472-7261-43CE-924B-ACCF719C32D9"), # Corbeille des fichiers stockés sur un lecteur réseau déconnecté
- [Guid]::Parse("00BCFC5A-ED94-4e48-96A1-3F6217F21990"), # Corbeille des captures d'écran
- [Guid]::Parse("6D809377-6AF0-444B-8957-A3773F02200E"), # Corbeille de la musique
- [Guid]::Parse("FD228CB7-AE11-4AE3-864C-16F3910AB8FE"), # Corbeille des images
- [Guid]::Parse("B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5"), # Corbeille des vidéos
- [Guid]::Parse("9B174B35-40FF-11D2-A27E-00C04FC30871"), # Corbeille des fichiers Internet
- [Guid]::Parse("724EF170-A42D-4FEF-9F26-B60E846FBA4F"), # Corbeille des fichiers temporaires
- [Guid]::Parse("7B0DB17D-9CD2-4A93-9733-46CC89022E7C"), # Corbeille des fichiers temporaires de l'utilisateur courant
- [Guid]::Parse("0D4C3DB6-03A3-462F-A0E6-08924C41B5D4"), # Recycle Bin for the current user's pictures library.
- [Guid]::Parse("2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39"), # Recycle Bin for the current user's music library.
- [Guid]::Parse("F1B6C905-9A9C-47A9-802D-819EEFF47D67"), # Recycle Bin for the current user's videos library.
- [Guid]::Parse("A5A9F9CF-357D-4FDE-A6FE-821E8E5876C3"), # GUID for the recycle bin on the selected drive
- [Guid]::Parse("9E52AB10-F80D-49DF-ACB8-4330F5687855"), # Recycle bin for CDs/DVDs
- [Guid]::Parse("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7"), # Recycle bin for external drives
- [Guid]::Parse("E2FADA1C-69B3-4E7B-A4C1-172422B3B19F"), # Recycle bin for flash drives
- [Guid]::Parse("DE4B071F-69AD-41E6-9A9D-3A8D1B3755FF") # Recycle bin for a specific user (where {SID} is the user's security identifier)
- )
- $dateComparisons = @(
- @{Label="-lt (Get-Date)"; Value={"-lt (Get-Date)"}},
- @{Label="-le (Get-Date)"; Value={"-le (Get-Date)"}},
- @{Label="-eq (Get-Date)"; Value={"-eq (Get-Date)"}},
- @{Label="-ge (Get-Date)"; Value={"-ge (Get-Date)"}},
- @{Label="-gt (Get-Date)"; Value={"-gt (Get-Date)"}},
- @{Label="-lt (Get-Date).AddDays(-7)"; Value={"-lt (Get-Date).AddDays(-7)"}},
- @{Label="-lt (Get-Date).AddDays(-30)"; Value={"-lt (Get-Date).AddDays(-30)"}},
- @{Label="-lt (Get-Date).AddDays(-365)"; Value={"-lt (Get-Date).AddDays(-365)"}},
- @{Label="-lt (Get-Date).AddMonths(-1)"; Value={"-lt (Get-Date).AddMonths(-1)"}},
- @{Label="-ge (Get-Date).AddYears(-11)"; Value={"-it (Get-Date).AddYears(-1)"}},
- @{Label="-ge (Get-Date).AddYears(-11)"; Value={"-ge (Get-Date).AddYears(-11)"}})
- if ([Environment]::OSVersion.Version.Major -ge 6) {
- $FOLDERID_RecycleBin += [Guid]::Parse("D574DE98-77DC-4b7f-9464-C394CFDD09DA") # Corbeille des applications
- }
- if ([Environment]::OSVersion.Version.Major -ge 10) {
- $FOLDERID_RecycleBin += [Guid]::Parse("0D4C3DB6-03A3-462F-A0E6-08924C41B5D4") # Corbeille des fichiers temporaires système
- }
- # Importer la bibliothèque Windows API
- Add-Type -Namespace Win32 -Name "NativeMethods" -MemberDefinition @"
- [DllImport("shell32.dll")]
- public static extern int SHGetKnownFolderPath([In] ref Guid rfid, [In] uint dwFlags, [In] IntPtr hToken, [Out] out IntPtr pszPath);
- "@
- # Importer la DLL nécessaire pour appeler la fonction SHGetKnownFolderPath
- Add-Type -AssemblyName System.Runtime.InteropServices
- # Récupérer les chemins des dossiers de la corbeille en utilisant la méthode SHGetKnownFolderPath
- $recycleBinPaths = foreach ($guid in $FOLDERID_RecycleBin) {
- $recycleBinPathPtr = [IntPtr]::Zero
- [Win32.NativeMethods]::SHGetKnownFolderPath([ref]$guid, 0, [IntPtr]::Zero, [ref]$recycleBinPathPtr) | Out-Null
- $recycleBinPath = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($recycleBinPathPtr)
- [System.Runtime.InteropServices.Marshal]::FreeCoTaskMem($recycleBinPathPtr) | Out-Null
- $recycleBinPath
- }
- Add-Type -AssemblyName System.Windows.Forms
- $form = New-Object System.Windows.Forms.Form
- $form.Text = "Select a Drive and Command"
- $form.ClientSize = New-Object System.Drawing.Size(400, 100)
- $form.StartPosition = "CenterScreen"
- $driveLabel = New-Object System.Windows.Forms.Label
- $driveLabel.Location = New-Object System.Drawing.Point(10, 10)
- $driveLabel.Size = New-Object System.Drawing.Size(80, 20)
- $driveLabel.Text = "Select a drive:"
- $form.Controls.Add($driveLabel)
- $driveComboBox = New-Object System.Windows.Forms.ComboBox
- $driveComboBox.Location = New-Object System.Drawing.Point(100, 10)
- $driveComboBox.Size = New-Object System.Drawing.Size(200, 25)
- $driveComboBoxx = New-Object System.Windows.Forms.ComboBox
- $driveComboBoxx.Location = New-Object System.Drawing.Point(100, 10)
- $driveComboBoxx.Size = New-Object System.Drawing.Size(200, 25)
- # Get local and removable disks
- $disks = Get-CimInstance Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 -or $_.DriveType -eq 2 }
- # Add each disk to the combo box
- foreach ($disk in $disks) {
- $driveComboBox.Items.Add($disk.DeviceID)
- }
- $form.Controls.Add($driveComboBox)
- $commandLabel = New-Object System.Windows.Forms.Label
- $commandLabel.Location = New-Object System.Drawing.Point(10, 40)
- $commandLabel.Size = New-Object System.Drawing.Size(90, 20)
- $commandLabel.Text = "Select a command:"
- $form.Controls.Add($commandLabel)
- $commandComboBox = New-Object System.Windows.Forms.ComboBox
- $commandComboBox.Location = New-Object System.Drawing.Point(100, 40)
- $commandComboBox.Size = New-Object System.Drawing.Size(200, 25)
- $commandComboBox.Items.Add("All Files")
- $commandComboBox.Items.Add("All Directories")
- $commandComboBox.Items.Add("All Directories and Files")
- $form.Controls.Add($commandComboBox)
- # $commandLabel = New-Object System.Windows.Forms.Label
- # $commandLabel.Location = New-Object System.Drawing.Point(10, 40)
- # $commandLabel.Size = New-Object System.Drawing.Size(90, 20)
- # $commandLabel.Text = "Select mod:"
- # $form.Controls.Add($commandLabel)
- # $ComboBox = New-Object System.Windows.Forms.ComboBox
- # $ComboBox.Location = New-Object System.Drawing.Point(10, 60)
- # $ComboBox.Size = New-Object System.Drawing.Size(200, 30)
- # $dateComparisons | ForEach-Object {
- # $ComboBox.Items.Add($_.Label) | Out-Null
- # }
- # $ComboBox.SelectedIndex = 0
- # $form.Controls.Add($ComboBox)
- # Add an OK button to close the window
- $okButton = New-Object System.Windows.Forms.Button
- $okButton.Location = New-Object System.Drawing.Point(310, 10)
- $okButton.Size = New-Object System.Drawing.Size(70, 55)
- $okButton.Text = "OK"
- $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
- $form.AcceptButton = $okButton
- $form.Controls.Add($okButton)
- $selectedDrive = @()
- function Show-DeletedFiles {
- param (
- [Parameter(Mandatory=$true, Position=0)]
- [System.Collections.ArrayList]$DeletedFiles
- )
- $table = @()
- foreach ($file in $DeletedFiles) {
- $row = [PSCustomObject]@{
- Name = $file.Name
- Type = $file.Extension
- OriginalLocation = $file.DirectoryName
- Size = $file.Length
- DeletedDate = $file.LastWriteTime
- IsFolder = $file.Attributes -eq 'Directory'
- }
- $table += $row
- }
- $table | Format-Table -AutoSize
- }
- #selectedComparison = @()
- $deletedFiles = @()
- $excludedFolders = @("FileCache", "ModuleCache") # Add the names of any folders to exclude from the search
- if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
- # Get the selected drive
- $selectedDrive = $driveComboBox.SelectedItem.ToString()
- Write-Host "The selected drive is $selectedDrive"
- $recycleBinPath = $null
- $recycleBinPath = Join-Path -Path $selectedDrive -ChildPath $RecycleBin
- #$selectedComparison = $dateComparisons[$comparisonComboBox.SelectedIndex].Value
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -eq 'Directory' -and $_.Name -match $Recycle.Bin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Recurse -Force -ErrorAction SilentlyContinue -Include *.* -File -Exclude $excludedFolders
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force -Recurse | Where-Object { $_.Name -match $RecycleBin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.Name -match $RecycleBin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- #$recycleBinFolders += Get-ChildItem -Path $recycleBinPath -Force -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -ne 'Directory' -and $_.Name -match $RecycleBin }
- #$recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- $recycleBinFolders = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- #| Where-Object { $_.Name -match $Recycle.Bin }
- # Check if the Recycle Bin path exists
- if ($recycleBinFolders.Count -gt 0 -and (Test-Path $recycleBinFolders[0].FullName)) {
- # Get all deleted files or directories in the Recycle Bin, including hidden items
- if ($commandComboBox.SelectedIndex -eq 0) {
- $deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Force -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.PSIsContainer -eq $false }
- } elseif ($commandComboBox.SelectedIndex -eq 1) {
- $deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Force -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.PSIsContainer -eq $true }
- } elseif ($commandComboBox.SelectedIndex -eq 2) {
- $recycleBinPath = Join-Path -Path $selectedDrive -ChildPath $RecycleBin
- #$deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- #$deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Attributes -eq 'Directory' -and $_.Name -match $Recycle.Bin }
- #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) } #246331
- #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) } #252687
- #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) } #242601
- #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -File -Force | Where-Object {$_.LastWriteTime -ge (Get-Date).AddYears(-11)} #245155
- #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -File -Force | Where-Object {$_.LastWriteTime -ge (Get-Date).AddYears(-11)} #221759
- #$deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force -Attributes Directory, Archive | Where-Object { $_.LastWriteTime -lt (Get-Date) } #212263
- #$deletedItems = Get-ChildItem -Path $recycleBinFolders[0].FullName -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) } #246341
- #$recycleBinPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop), $RecycleBin)
- # $deletedItems = Get-ChildItem -Path $recycleBinPath -Recurse -Force -Include *.* -File | Where-Object { $_.LastWriteTime -lt (Get-Date) }
- $deletedItems = Get-ChildItem -Path $recycleBinPath -Force | Where-Object { $_.LastWriteTime -lt (Get-Date) -and $_.Name -match $RecycleBin }
- }
- if ($deletedItems.Count -gt 0) {
- Write-Host "$($deletedItems.Count) deleted items found in the Recycle Bin."
- } else {
- Write-Host "No deleted items found in the Recycle Bin."
- }
- } elseif ($commandComboBox.SelectedIndex -eq 1) {
- $Shell = New-Object -ComObject Shell.Application
- $recycleBinFolder = $Shell.Namespace(0xa)
- $recycleBinItems = $recycleBinFolder.Items()
- $deletedItems = @()
- foreach ($item in $recycleBinItems) {
- if ($commandComboBox.SelectedIndex -eq 0 -and $item.IsFolder -eq $false) {
- $deletedItem = [PSCustomObject]@{
- Name = $item.Name
- Type = $recycleBinFolder.GetDetailsOf($item, 0)
- OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
- Size = $recycleBinFolder.GetDetailsOf($item, 2)
- DeletedDate = ($recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder')
- IsFolder = ($recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder')
- }
- $deletedItems += $deletedItem
- } elseif ($commandComboBox.SelectedIndex -eq 1 -and $item.IsFolder -ne $true) {
- $deletedItem = [PSCustomObject]@{
- Name = $item.Name
- Type = $recycleBinFolder.GetDetailsOf($item, 0)
- OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
- Size = $recycleBinFolder.GetDetailsOf($item, 2)
- DeletedDate = ($recycleBinFolder.GetDetailsOf($item, 4) -ne 'Folder')
- IsFolder = ($recycleBinFolder.GetDetailsOf($item, 4) -ne 'Folder')
- }
- $deletedItems += $deletedItem
- }
- elseif ($commandComboBox.SelectedIndex -eq 2) {
- $deletedItem = [PSCustomObject]@{
- Name = $item.Name
- Type = $recycleBinFolder.GetDetailsOf($item, 0)
- OriginalLocation = $recycleBinFolder.GetDetailsOf($item, 1)
- Size = $recycleBinFolder.GetDetailsOf($item, 2)
- DeletedDate = $recycleBinFolder.GetDetailsOf($item, 4)
- IsFolder = $recycleBinFolder.GetDetailsOf($item, 3) -eq 'Folder'
- }
- $deletedItems += $deletedItem
- }
- }
- }
- # Show the deleted files in a table
- if ($deletedItems.Count -gt 0) {
- # Append the new items to the existing array
- $deletedFiles += $deletedItems
- }
- if ($deletedFiles.Count -gt 0) {
- Show-DeletedFiles $deletedFiles
- } else {
- Write-Host "No items found in the Recycle Bin."
- }
- }
- if ($deletedFiles -ne $null) {
- $deletedItems = $deletedFiles
- }
- # Définir le filtre de fichier pour le SaveFileDialog
- $saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
- $saveFileDialog.Filter = "Tous les fichiers (*.*)|*.*"
- $saveFileDialog.Title = "Sélectionner un emplacement de restauration"
- # Si l'utilisateur sélectionne un fichier à restaurer, ouvrir le SaveFileDialog pour sélectionner un emplacement de restauration
- if ($deletedItems.Count -gt 0) {
- $deletedItem = $deletedItems | Out-GridView -Title "Sélectionner un fichier à restaurer" -PassThru
- if ($deletedItem) {
- $fileToRestore = $deletedItem.Name
- $saveFileDialog.FileName = $fileToRestore
- $saveFileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($fileToRestore)
- $saveFileDialog.DefaultExt = [System.IO.Path]::GetExtension($fileToRestore)
- $saveFileDialog.CheckPathExists = $true
- if ($saveFileDialog.ShowDialog() -eq 'OK') {
- $restorePath = $saveFileDialog.FileName
- Copy-Item -Path $deletedItem.FullName -Destination $restorePath
- Write-Output "Le fichier $fileToRestore a été restauré avec succès à l'emplacement $restorePath."
- }
- }
- }
- else {
- Write-Output "Aucun fichier supprimé n'a été trouvé dans la corbeille."
- }
- Pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement