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.





PowerShellDSC and Azure: more notes

This is starting to feel a little more robust in my mind… I created a pair of VMs on Azure, on the same subnet, with 1 implicitly a push server, and 1 a “client” server, or “pushee”. Each have public IPs… although thinking about it could I just have opened the RDP ports without that? Don’t know, don’t think so. But regardless they talk DSC to each other through their respective private ports. In fact I couldn’t actually get them to talk DSC/PowerShell/WsMan to each other through their public ports/DNS names. Authentication is done through runtime credentials… which are the same on both servers, FWIW. I don’t have a domain (uksouth.cloudapp.azure.com not really a domain… and if it was that would worry me from a security point of view.

The test here was to create a folder from one to the other, being the very simplest proof that had value… and it worked.

Advice: after each failed run, delete the created Dsc configuration folder. Avoids confusion.


PowerShell: dates and -replace

I sometimes need to do a naive replacement of dates in a typical UK format to a typical US format.

For example, I might want to take

The Battle of Hastings took place on 14/10/1066 between Harold and William. The war between the 2 was triggered by Edward the Confessor’s death on 12/06/1066.

, and convert it to:

The Battle of Hastings took place on 10/14/1066 between Harold and William. The war between the 2 was triggered by Edward the Confessor’s death on 06/12/1066

or to…

The Battle of Hastings took place on 10-14-1066 between Harold and William. The war between the 2 was triggered by Edward the Confessor’s death on 06-12-1066

This line achieves the second transformation, assuming the text is in history.txt. Note the single quotes.

$(Get-Content -Path .\history.txt) -replace '([0-9]{2})/+([0-9]{2})/+([0-9]{4})','$2-$1-$3'



Credit to Don Jones for a similar example.

NotePad++:  As this is essentially Regex after all, I will mention that you can do the same kind of thing in NotePad++ and indeed in anything that has a Regex capability. The NotePad++ case looks like this, where the “Find what” pattern is exactly as for PowerShell, and the “Replace with” differs a bit: [\2/\1/\3]


Chef and Azure: learn.chef.io

I had not looked at the Chef docs for some time. They continue to be excellent. I was using them to see how deploying to Azure differed from a more on-premise VM. As far as I could see, the only difference was making sure that you had enabled the 80,5985 and 443 endpoints/ports in the Azure Portal. No amount of opening firewall ports on the provisioned VM will help if you have not done that.

I also suggest getting things working with the simplest possible test, and making network calls within a single server to start (I know what I mean hopefully you do, too late to explain more 🙂 )

I left comments here and here.

A very helpful post on WinRm and networking difficulties here.

Easiest way to get non-truncated history, btw (hm not so sure now):

h | select -Property CommandLine -unique > x.txt

Session edits.



Net User syntax on Windows 10

And doubtless it is the same on Windows 7, 8.1, etc. and possibly on Windows Server.


The context for “playing” with user creation and rights was a struggle to get PowerShell (PSSession), and PS DSC talking across 2 PCs on the same HomeGroup. While insecure, I was hoping, for testing purposes only, to get away without the need to share certificates etc.

In doing this, I learnt that the built-in administrator user is not visible by default. Also while you can create and delete users, there seems no way of elevating them to admin rights. So right now if I need an made-up admin user, I can create them here, but elevate them in the GUI.