List and delete files using Windows PowerShell

2016/04/18 17:17 -



To list files of specific type(s) on the disk or directory including all sub-directories

get-childitem c:\ -include *.tmp, *.temp, *.bak -recurse 

get-childitem D:\Projects\abc*\ -include *.bak -recurse 

The commands above will list on screen all files with extentions TMP, TEMP or BAK located on disk C:\ and all subdirectories or located on disk D:\ in directory PROJECTS and all subdirectories with name starting with ABC and their subdirectories.

To delete files of specific type(s) from the disk or directoty including all sub-directories add

| foreach ($_) {remove-item $_.fullname}
 

to the end of the previous command

get-childitem c:\ -include *.tmp, *.temp, *.bak -recurse | foreach ($_) {remove-item $_.fullname}

get-childitem D:\Projects\abc*\ -include *.bak -recurse | foreach ($_) {remove-item $_.fullname}

to list what would be deleted if the command above is executed, but without deleting the files add "-whatif" parameter

get-childitem D:\Projects\abc*\ -include *.bak -recurse | foreach ($_) {remove-item $_.fullname -whatif}

To write the file list to file on the disk instead of displaying on the screen add "Out-File file_name -width 160".

get-childitem D:\Projects\abc*\ -include *.bak -recurse | Out-File C:\Temp\bak_files_list.txt -width 160

The parameter "width 160" sets the width of the line in the text file to 160 characters.