0

I use the Remove-Item -Recurse -Force command to remove large folders since the method seems much faster that using the Windows explorer, but a path is required. After adding a "open Powershell here" entry to the Windows context menu being able to remove the current folder would make deleting sizable directories almost seamless.

a Powershell window

4
  • 2
    As you cannot delete the directory you are in you will need to set your location to a variable, then change directory and then delet the previous directory, the following should do what you want: $CurrentDir = Get-Location; cd ..; Remove-Item $CurrentDir -Recurse -Force
    – CraftyB
    Jun 11, 2020 at 18:24
  • @CraftyB this sounds like an answer to me. Why not write one?
    – LPChip
    Jun 11, 2020 at 21:16
  • OK, you've shown us the window that appears, but not the command you type nor the error/result. Your question needs to be edited or no one can help you. Jun 11, 2020 at 23:15
  • @KeithMiller: I've updated the question (albeit you answered), I'm asked for path.
    – user198350
    Jun 12, 2020 at 9:47

2 Answers 2

2

So, save the current location, move up one level, delete saved location.

Verbose:

$Path = Get-Location | Select -expand Path
Set-Location ..
Remove-Item -LiteralPath $Path -Recurse -Force

Key-banger:

$p = (gl).Path
sl ..
ri -LiteralPath $p -r -Fo

Create a function with an alias & add it to your profile if you want a single command easily available:

@'
Function Remove-Location {
   $Path = Get-Location | Select -expand Path
   Set-Location ..
   Remove-Item -LiteralPath $Path -Recurse -Force
   Exit    
}
Set-Alias rl Remove-Location
'@ | Add-Content $PROFILE -Append

Then you can open PowerShell to any folder, type rl, and the folder will be removed.

7
  • I haven't tested the alias yet, but accepted based on the command. Is it possible to make the Powershell window close once finished?
    – user198350
    Jun 12, 2020 at 9:41
  • Yes, simply add an Exit statement to the Function definiton. I'll edit my answer to include that. Jun 12, 2020 at 21:02
  • @user598527 just keep in mind that the exit will always close your powershell window after you executed the command. I'd stick with writing exit yourself after you executed the command
    – SimonS
    Jun 14, 2020 at 17:27
  • The command seems to struggle at least with brackets [].
    – user198350
    Jun 19, 2020 at 16:24
  • 1
    See edited code. Now using -LiteralPath. Jun 19, 2020 at 16:30
0

If you wanted to handle this easily within the console the following command will set the current directory to a variable and then change to the parent directory and delete the previous directory:

$CurrentDir = Get-Location; cd ..; Remove-Item $CurrentDir -Recurse -Force

If you are feeling a bit more adventurous you could add an entry to the context menu so you do not need to interact with the shell altogether:

Only continue with this if you are confident in using the registry editor, making mistakes could have irreversible effects

Run RegEdit.exe from the run box.

Navigate to HKEY_CLASSES_ROOT\Directory\shell\ in the Registry Editor

"Right Click" on the shell folder and click "New" -> "Key"

Enter a name for the new key for the extension to the context menu in my case I used PSMenu. (I would name it the same for the simplicity of following this).

When inside the new key you will see a value on the right named (Default), double click into this and modify the "Value Data" to what you want it to appear as in the context menu in my case I have used Delete folder using PowerShell then click "OK".

Next to make the command only visible on shift + right-click you will need to create a new value. Whilst inside the new key right-click in the right pane and click "New" - "String Value", name this value as Extended and do not populate the "Value Data".

Do not close the registry editor but jump to desktop etc, now when you Shift + Right-Click you should see the new entry that you have just created, though this is currently not hooked up to a command.

To add the command to the context entry return back to Registry Editor and Navigate to the Key "PSMenu" (Or what ever you named it). Right-click on the key in the left pane and "click" "New" -> "Key" and name this command, now select this key. Again you should have only a single value in the right pane named (Default), double click this and paste the following into the Value Data:

C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Remove-Item -LiteralPath '%L' -Recurse -Force; Exit

Click "ok" and then exit Registry Editor, the context menu entry you have created should now work and delete the folder you Shift + Right-Click and select that entry on.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .