Powershell - How to use "remove-item" with multiple selectors and wildcards?

I have a folder called "test-folder" which contains alot of files with different types. I want to delete all music files. (mp3, mp4, mpeg ...)

I know i can delete all mp3 files like this:

remove-item C:\path\to\test-folder\"*.mp3*"

Is it possible to add multiple wildcard selectors e.g.:

remove-item C:\path\to\test-folder\"*.mp3*+*.mpeg*"

so i can delete all the music files with one command?

3 Answers

For a single directory:

remove-item C:\path\to\test-folder\* -include *.mp3, *.mpeg

or a useful method for when files span multiple directories:

remove-item C:\path\to\test-folder\*.mp3, C:\path\to\other\test-folder\*.mpeg

or you could move to that directory first:

cd C:\path\to\test-folder\
remove-item *.mp3, *.mpeg

Use Get-Help Remove-Item -full for full details of available flags and usage.

1
remove-item C:\path\to\test\folder\* -include .mp4,.mp3

The above command needs to changed a little to work correctly (courtesy @root)

remove-item C:\path\to\test\folder\* -include *.mp4,*.mp3
3

For the opposite case, I want to remove all of the ArtWork, db, Zune, desktop files from the Music folders. We need to use -Recurse for network drive NAS1:

\\NAS1\Public> remove-item '.\Shared Music\*' -include desktop.ini,_msl.db,Zune*.jpg,Folder.jpg,AlbumArt*.jpg -Recurse

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like