Functional and Classless OO programming in PowerShell

Hi PSO’ers,

I’m learning JavaScript and this is taking the form of classless object-orientation via Closures (not classical or prototypal inheritance) and wanted to use similar patterns in PowerShell, if possible.

Right now I am attempting to write a script in PowerShell to check the Windows 10 Assets directory to see if any new wallpaper images have been added. Apparently, PowerShell is, like JavaScript, a multi-paradigm scripting language - in that it provides the programmer the ability to use both OO and FP models. And similar to JavaScript, PowerShell is a classless OO language.

So, I’m looking for tips, recommendations, etc, from those who know both languages. I’m not looking for code offerings, as I’d like to research what people suggest here and develop my own. Although, pointers to code would be appreciated.

I’d like the script to initially do the following:

  • 1. create a service (preferably) that will monitor the "C:\Users$env:username\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" directory
    2. confirm the existence of the directory
    3. notify the script/service to perform the following when new files are added
    4. scan the directory and (initially) create a JSON (or, if easier, a PSON file) to hold a history of all parsed files
    5. extract the “name”, “dimension”, and “date created”, of all new files into the JSON/PSON file
    6. evaluate all file names and creation dates against this file to establish a list of new files
    7. copy all new files into different directories according to their dimension
    8. rename all files copied to the new directories (one for Desktop and one for Mobile) by adding a suffix of “.jpg”

  • The goal of the script is to use a service to interact with System.IO.FileSystemWatcher and capture all new images.

    This is an existing PS script that uses FSW:

    I’ve also come across these scripts/services written in C#:
    http://www.codeproject.com/Articles/58740/FileSystemWatcher-Pure-Chaos-Part-of

    However, I would like to not use objects based on classes in PowerShell, and instead base them on functions and Closures. I may be going about this the wrong way, so any feedback would be appreciated.

    Thanks!

    Here are some basic examples of Map and Fold functions. I have messed around with passing around anonymous functions as values, but I haven’t found it to be really any better than advanced functions leveraging the pipeline.