# ‘Clear-Recyclebin’ Is Not Recognized As The Name Of A Cmdlet In Powershell Core

Recently I have upgraded to PowerShell Core and slowly switching from Windows PowerShell to PowerShell Core. I have noticed quite a few CmdLets are missing in the PowerShell Core, since it became an open source and supports on cross-platform most of the platform dependent CmdLets won’t work on the other platforms. I usually clear my temp folders and recyclebin in all my computers frequently, and noticed Clear-RecycleBin CmdLet is not a valid CmdLet in PowerShell Core…

Clear-RecycleBin : The term 'Clear-RecycleBin' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Clear-RecycleBin -Force
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Clear-RecycleBin:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


So I have decided to create a PowerShell function to achieve almost the the same functionality of Clear-RecycleBin using .NET class, and here is the function…

Function Empty-RecycleBin
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact = 'High')] param ( [Parameter(Mandatory=$false)]
[switch] $Force # Without confirmation ) if($IsWindows -eq $false) { return } # Exit the script if the OS is other than Windows # Since the Crear-RecycleBin CmdLet is not availble on PowerShell Core, # achive the same functionality using the .Net Classes.$Type = @'
using System;
using System.Runtime.InteropServices;
namespace MyComputer
{
public static class RecycleBin
{
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, int dwFlags);
public static void Empty()
{
SHEmptyRecycleBin(IntPtr.Zero, null, 1);
}
}
}
'@
Add-Type -TypeDefinition $Type # Bypass confirmation, and empty the recyclebin if ($PSBoundParameters.ContainsKey('Force'))
{
[MyComputer.RecycleBin]::Empty()
return
}

# Default behaviour, with confirmation empty the recyclebin
if(\$PSCmdlet.ShouldProcess('All of the contents of the Recycle Bin','Empty-RecycleBin')){
[MyComputer.RecycleBin]::Empty()
return
}
}


Output:

I have not added the -DriveLetter flag, since I want to clear the recyclebin from all the drives, if you want clear the recyclebin from a specific drive, you need add the driveLetter argument to the Empty method in the C# code and add the -DriveLetter parameter to the PowerShell function.

Share it on     |   |   |   |