Reputation:
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!
Upvotes: 3
Views: 7260
Reputation: 437638
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
.
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.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.
Upvotes: 7
Reputation: 826
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.
Upvotes: 0