Skip to content

Instantly share code, notes, and snippets.

@everydayintech
Created April 2, 2024 19:13
Show Gist options
  • Select an option

  • Save everydayintech/e2da7b9e7e9257fee08d5a6633c1e8b0 to your computer and use it in GitHub Desktop.

Select an option

Save everydayintech/e2da7b9e7e9257fee08d5a6633c1e8b0 to your computer and use it in GitHub Desktop.
Moves a file or directory to the Recycle Bin
<#
.SYNOPSIS
Moves a file or directory to the Recycle Bin.
.DESCRIPTION
The Remove-ItemToRecycleBin function moves a specified file or directory to the Recycle Bin.
It uses the Microsoft.VisualBasic.FileIO.FileSystem class to perform the operation.
Original author: Ste (https://stackoverflow.com/users/8262102/ste)
Improved by: everydayintech (github.com/everydayintech)
.PARAMETER LiteralPath
Specifies the path of the file or directory to be moved to the Recycle Bin.
This parameter is mandatory.
.PARAMETER WhatIf
A switch parameter that allows the user to test the deletion first.
If this switch is provided, the function will only display what would happen if the operation was performed, without actually doing it.
.EXAMPLE
Remove-ItemToRecycleBin -LiteralPath "C:\temp\myFile.txt"
Moves the file "myFile.txt" from the "C:\temp" directory to the Recycle Bin.
.EXAMPLE
Remove-ItemToRecycleBin -LiteralPath "C:\temp\myFile.txt" -WhatIf
Displays what would happen if the "myFile.txt" file was moved to the Recycle Bin, without actually doing it.
.EXAMPLE
Get-ChildItem -Path "C:\temp" | Remove-ItemToRecycleBin
Moves all files and directories in the "C:\temp" directory to the Recycle Bin.
.NOTES
The function uses the Microsoft.VisualBasic.FileIO.FileSystem class to move items to the Recycle Bin.
Therefore, it requires the Microsoft.VisualBasic assembly to be loaded.
.LINK
For more information about the Microsoft.VisualBasic.FileIO.FileSystem class, see:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.filesystem
#>
function Remove-ItemToRecycleBin {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, HelpMessage = 'Directory path of file path for deletion.', ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[String]$LiteralPath,
[Parameter(Mandatory = $false, HelpMessage = 'Switch for allowing the user to test the deletion first.')]
[Switch]$WhatIf
)
Begin {
Add-Type -AssemblyName Microsoft.VisualBasic
}
Process {
$item = Get-Item -LiteralPath $LiteralPath -ErrorAction SilentlyContinue
if ($null -eq $item) {
Write-Error ("'{0}' not found" -f $LiteralPath)
}
else {
$fullpath = $item.FullName
if (Test-Path -LiteralPath $fullpath -PathType Container) {
if (!$WhatIf) {
Write-Verbose ("Moving '{0}' folder to the Recycle Bin" -f $fullpath)
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath, 'OnlyErrorDialogs', 'SendToRecycleBin')
}
else {
Write-Host "Testing deletion of folder: $fullpath"
}
}
else {
if (!$WhatIf) {
Write-Verbose ("Moving '{0}' file to the Recycle Bin" -f $fullpath)
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath, 'OnlyErrorDialogs', 'SendToRecycleBin')
}
else {
Write-Host "Testing deletion of file: $fullpath"
}
}
}
}
End { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment