bdukes / PowerShellModules

A collection of PowerShell modules
Apache License 2.0
44 stars 9 forks source link

use in pipe #6

Closed nopria closed 3 years ago

nopria commented 3 years ago

I use

get-childitem | where-object { $_.Name -match 'myregex' } | remove-item

to delete files, so I installed the Recycle module hoping I could just replace remove-item with Remove-ItemSafely

but I got the following error:

Get-Item : Impossibile associare l'argomento al parametro 'Path' perché è null.
In C:\Program Files\WindowsPowerShell\Modules\Recycle\1.2.0\Recycle.psm1:59 car:47
+ ...                 $items = @(Get-Item -Path:$PSBoundParameters['Path'])
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Item], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand

Is it an issue or simply I cannot use Remove-ItemSafely in a pipe?

bdukes commented 3 years ago

Thanks for the question. The way that I was using the pipeline didn't work with piped files, but I've pushed Recycle 1.3.0 which I've adjusted to work correctly with piped files. Let me know if you run into any issues with it. Thanks!

nopria commented 3 years ago

Thanks, now it works, but it is asking for confirmations one file by one (due to piping?), so if you have many files, like in my case, it is impossible to use in practice. Is it possible to avoid confirmations?

bdukes commented 3 years ago

It shouldn't be asking for confirmations unless you're passing -Confirm. Do you get a single confirmation from Remove-Item?

bdukes commented 3 years ago

Does the Yes to All option not work in the confirmation? I'm seeing the same behavior between Remove-Item and Remove-ItemSafely.

nopria commented 3 years ago

No, I don't get any confirmation from Remove-Item.

yes-to-all is a Remove-ItemSafely option? I tried Remove-ItemSafely -yestoall and Remove-ItemSafely -yes-to-all but I got errors.

bdukes commented 3 years ago

Oh, are you talking about the confirmation dialog you need to click in Windows? That's a Windows setting that you'll need to adjust. This cmdlet deletes the file as though you had deleted it through File Explorer, so if there's a confirmation when deleting via File Explorer, it'll show here, also.

If you right click on the Recycle Bin and select Properties, there's a Display delete confirmation dialog option which you can uncheck. This will avoid giving you a confirmation about moving files to the Recycle Bin. Recycle Bin properties screenshot

Hope that helps!

nopria commented 3 years ago

Thanks, disabling temporarily the confirmation dialog system-wide can be a workaround because, except when I delete files through PowerShell regex, I want such confirmation dialog enabled.

However it would be much better to have an option doing that just for the Remove-ItemSafely command.

bdukes commented 3 years ago

I'm sorry, my understanding is that the API I'm using doesn't give any option to suppress that warning.

Spykerwolf commented 3 years ago

Would the following be another workaround? I haven't tried it myself, but found it while searching:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('d:\foo.txt','OnlyErrorDialogs','SendToRecycleBin')
bdukes commented 3 years ago

That does looks like a potential option someone could explore

Mortyfar commented 2 years ago

Hello @bdukes I have just found this module, and I was having the same issue with the popup dialog. Your solution to uncheck the confirmation dialog in the recycle bin properties did solve my problem, but... I was trying to circumvent the dialog with the -force parameter, witch for some reason did not put the item in the bin, but deleted the item permanently. Is there any logical explanation for this?

bdukes commented 2 years ago

The current design of the Cmdlet is that if parameters are passed in that aren't implemented by Remove-ItemSafely then if falls back to Remove-Item. The way it's currently implemented, there wouldn't be a way for -Force to do anything differently. I'm sorry if that fallback caused trouble for you.

https://github.com/bdukes/PowerShellModules/blob/91d64c09e18efcfaf26cbd82c60cbb91242a3370/Recycle/Recycle.psm1#L46