PowershellIT #2 – Remove User Files

This is one of the posts from the PoweshellIT series in which we get common and sometimes not so common usecases and try to simplify/automate them using PowerShell.

Today’s Use Case

Delete all common file types(documents, media and pictures – basically user file), except for protected folders in the C:\windows directory. The intent is to prevent users from saving and retaining files from the web on a publicly accessible computer.

Infrastructure overview

Public Computer running Windows 10 Pro.

Disclaimer: we are considering only Powershell scripting as a possible solution not trying to redesign the whole infrastructure or implement kiosk mode.

Context

Looks like a pretty easy task. Detailed research (first page of Google search) about common data types to be removed yielded 6 personal file categories:

  • Presentation file formats
  • Audio file formats
  • Spreadsheet file formats
  • Video file formats
  • Word processor and text file formats
  • Image file formats

Here is detailed list with file extensions:

• Presentation file formats
.key - Keynote presentation
.odp - OpenOffice Impress presentation file
.pps - PowerPoint slide show
.ppt - PowerPoint presentation
.pptx - PowerPoint Open XML presentation

• Audio file formats
.aif - AIF audio file
.cda - CD audio track file
.mid or .midi - MIDI audio file.
.mp3 - MP3 audio file
.mpa - MPEG-2 audio file
.ogg - Ogg Vorbis audio file
.wav - WAV file
.wma - WMA audio file
.wpl - Windows Media Player playlist

• Spreadsheet file formats
.ods - OpenOffice Calc spreadsheet file
.xlr - Microsoft Works spreadsheet file
.xls - Microsoft Excel file
.xlsx - Microsoft Excel Open XML spreadsheet file

• Video file formats
.3g2 - 3GPP2 multimedia file
.3gp - 3GPP multimedia file
.avi - AVI file
.flv - Adobe Flash file
.h264 - H.264 video file
.m4v - Apple MP4 video file
.mkv - Matroska Multimedia Container
.mov - Apple QuickTime movie file
.mp4 - MPEG4 video file
.mpg or .mpeg - MPEG video file
.rm - RealMedia file
.swf - Shockwave flash file
.vob - DVD Video Object
.wmv - Windows Media Video file

• Word processor and text file formats
.doc and .docx - Microsoft Word file
.odt - OpenOffice Writer document file
.pdf - PDF file
.rtf - Rich Text Format
.tex - A LaTeX document file
.txt - Plain text file
.wks and .wps- Microsoft Works file
.wpd - WordPerfect document

• Image file formats
.ai - Adobe Illustrator file
.bmp - Bitmap image
.gif - GIF image
.ico - Icon file
.jpeg or .jpg - JPEG image
.png - PNG image
.ps - PostScript file
.psd - PSD image
.svg - Scalable Vector Graphics file
.tif or .tiff - TIFF image

Reference: https://www.computerhope.com/issues/ch001789.htm

Challenges

Removing item from whole computer and filtering by file type is pretty straight forward. We need to envision any possible changes (addition/removal) to common data types and make list of file types modular and easy to change.

Proposed solution

Json Config file with data types for ease of adding/removing them.
Simple PowerShell module with couple of functions referring config file.

Pseudo Code

Get config file with Personal File Types
Get All of Filesystem drives
foreach drive get all files with PersonalFileTypes extension
filter out excluded files & folders
remove not filtered files

It has been wrapped into PowerShell module called RemovePersonalFiles.

Module contains 4 functions:

  • Get-RemovePersonalFileConfig
  • Create-RemovePersonalFileDefaultConfig
  • Update-RemovePersonalFileConfig
  • Remove-PersonalFile

Get-RemovePersonalFileConfig

Get Remove Personal File current config. Returns PS custom object based on config file. Config file is a json file which contains collection of object with category and filetype. Filetype is also an object which has description and extension.

JSON Config File Example
{
     "Category":  "Presentation file formats",
     "FileType":  [

                      {
                          "extension":  "*.odp",
                          "description":  "OpenOffice Impress presentation file"
                      },

                      {
                          "extension":  "*.pptx",
                          "description":  "PowerPoint Open XML presentation"
                      }
                  ]
 }
EXAMPLE
Get-RemovePersonalFileConfig

Create-RemovePersonalFileDefaultConfig

Generate RemovePersonalFile default config. Default config file contains 6 categories and 56 personal file extensions.
Config File is saved at PS Module root location.

EXAMPLE
Create-RemovePersonalFileDefaultConfig

Update-RemovePersonalFileConfig

Update config file by adding additional category and file extensions.

EXAMPLE
$ImageFileFormats=[ordered]@{
             Category="Image file formats"
             FileType=@(
                 @{
                     description="Adobe Illustrator file"
                     extension="*.ai"
                 },
                 @{
                     description="Bitmap image"
                     extension="*.bmp"
                 },
                 @{
                     description="PNG image"
                     extension="*.png"
                 }
             )
         }

 Update-RemovePersonalFileConfig -ConfigObject $ImageFileFormats

Remove-PersonalFile

Remove personal files types specified in the Config file from all filesystem drives. Has only one optional parameter “Exclude” which specifies which folder should be excluded. Folder “C:\Windows” is excluded by default.

EXAMPLE
Remove-PersonalFile -Exclude "C:\Windows\"

All of the source code is available in PowerShellIT repository on the GitHub.

Thanks a lot for reading.

Icons made by Eucalyp & Good Ware from www.flaticon.com

One thought on “PowershellIT #2 – Remove User Files”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.