Removing Unwanted Files with PowerShell

Sometimes after using gVim on Windows, I end up with temporary files.

.\content\5-minute-intro-to-gradle.markdown
.\content\5-minute-intro-to-gradle.markdown~
.\content\command-line-diff-for-windows.markdown
.\content\command-line-diff-for-windows.markdown~

I’d really like to remove all the files in .\content that end in ~. If find on Unix immediately comes to mind, PowerShell’s Get-ChildItem will feel right at home.

Get-ChildItem -Path .\content -Filter *~ -File | Remove-Item

Our directory is free from unwanted files!

Note It’s also possible to directly use the Remove-Item command, but by using Get-ChildItem we can safely fine-tune our query before piping it to the destructive Remove-Item.


Published Sep 28, 2014