PowerShell: AVI to mp4 conversion, and related

I’ve been using Handbrake CLI wrapped in PowerShell to convert some bulky old AVI files to MP4, and then to copy those to DVD for another layer of backup.

Some scripts here, here, here and here.

I have just finished a pretty extensive repo and Wiki on this subject, on GitHub, here. The intended audience for this is those who can find their way around Windows, but who know nothing about PowerShell.

I’ve now made a video here to illustrate just the AVI to mp4 conversion part.


PowerShell: Get-RandomString

I’ve seen plenty of long-winded ways of getting a random string (not int) in PowerShell, but none of them seem to mention the obvious of taking a GUID, stripping of the dashes, and returning the required size. Obviously that restricts you a bit, but for my purposes, it is fine.

This is all there is to it:

function Get-RandomString ([int] $stringSize) {
    $randomString = [System.Guid]::NewGuid() -replace '-',''
    ([string] $randomString).Remove($stringSize)

And you call it thus…

    Get-RandomString 6

And get returned something like this:


Here’s another way, starting off with the 1 liner, and the breaking it down a bit:


 $randomString = ([char[]]([char]'a'..[char]'z') + 0..9 | Sort-Object {Get-Random})[0 .. 8] -join ''


PowerShell: call your own DLLs

Firstly credit for the syntax.

I want to write a library in C#, and invoke it from PowerShell. Let us also say this is a bare-bones Windows 7…10 PC, and therefore has no C# compiler, but obviously does have PowerShell natively, even if the versions vary (2-5).

C# Compiler

In fact every Windows PC has a C# compiler, in the framework folder, e.g. [C:\Windows\Microsoft.NET\Framework\v4.0.30319]. I did not want to add this to the path. I also wanted to understand the minimum set of C# artifacts to compile code. Turns out there are just 2 required: [csc.exe] and [cscui.dll]. I copied this into my PowerShell working folder. So let us say we have a library [MathLib.cs] (see the credit). You can compile this thus:

.\csc.exe /target:library MathLib.cs

, which results in this:


Load and call a DLL from PowerShell

You can then load it using…


and invoke it like this:

[MathLib.Methods]::Sum(10, 2)

, or load it like this:

$mathInstance = new-object MathLib.Methods

and invoke it like this:

$mathInstance.Product(10, 2)


Again, all credit to Lee Holmes – I’ve just taken what he did and re-presented for my preferences.