简体   繁体   中英

Error with recursive deletion in Windows

I am currently uninstalling some software that generated data for a large number of files, all .jpg and .nfo and I would like to delete these files. I am attempting to use PowerShell to perform this operation and the command I have tried is:

del /S *.jpg

And received the following as a result:

Remove-Item : A positional parameter cannot be found that accepts argument '*.jpg'.
At line:1 char:1
+ del /S *.jpg
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Thanks!

You're looking for:

Get-ChildItem {Path to root of folder to search from} -Recurse | Where-Object (($_.FullName -like "*.jpg") -or ($_.FullName -like "*.nfo")) | Remove-Item -Force

I'm yet to test this as I'm writing on my iPhone using the app, so if it's wrong let me know and I'll fix it up.

tl;dr

  • Either : Run your cmd.exe command as-is, by prefixing it with cmd --% /c :
    cmd --% /c del /S *.jpg
    --% , the stop-parsing symbol, tells PowerShell to stop parsing the remaining arguments and pass them through as-is (except for expanding %<name>% environment variable references) - see Get-Help about_Parsing .

  • Or : Preferably, use PowerShell's native Remove-Item cmdlet :
    Remove-Item * -Recurse -Include *.jpg -WhatIf
    Note how common parameter -WhatIf conveniently allows you to preview the operation before committing to it; see Get-Help Remove-Item .

    • Caution : As of PSv5.1, the Remove-Item , Copy-Item and Move-Item cmdlets and, to a lesser extent, Get-ChildItem are notoriously finicky when it comes to recursive operations and the -Include and -Exclude parameters - there are many subtleties.
      In general, however, PowerShell's cmdlets are far more powerful than their cmd.exe counterparts, and any effort to learn how to use them effectively is well spent - not least because you'll soon be able to use PowerShell on Unix platforms as well .

cmd.exe commands like del and dir are built into cmd.exe and cannot be called directly from PowerShell; to invoke them, you must invoke cmd.exe explicitly, using cmd /c , as shown above.

What may be confusing is that PowerShell defines aliases for its own commands (cmdlets) named for their roughly equivalent cmd.exe counterparts.
The real names of PowerShell cmdlets are very different, following a verbose, but very consistent naming scheme (that even governs naming of PowerShell-native aliases).

For instance, del is an alias for PowerShell's Remove-Item cmdlet.

This aliasing is a double-edged sword: on the one hand, it makes it easier for people who are used to cmd.exe (batch programming) to discover their PowerShell counterparts; on the other hand, it obscures the fact that PowerShell's command-line syntax is quite different .

To learn what command PowerShell actually invokes when you type a name, use Get-Command :

> Get-Command del

CommandType     Name                                               Version    Source                                                                           
-----------     ----                                               -------    ------                                                                           
Alias           del -> Remove-Item              
  • For a quick overview of a command's syntax, invoke it with -? or use Get-Help :
    del /?

  • For more extensive help, use Get-Help -detailed <cmd> or Get-Help -full <cmd> ; see Get-Help -detailed Get-Help .

As you can see, you can pass -? to and invoke Get-Help with the alias forms of commands as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM