The PowerShell Add-Type cmdlet lets you define a .NET Framework class in your Windows PowerShell session. It supports the following languages CSharp, CSharpVersion3, VisualBasic, JScript. It does not support FSharp directly.
No problem
Add-Type has a parameter CodeDomProvider and you can pass the FSharpCodeProvider to it (see the function Add-TypeFSharp).
Add-TypeFSharp lets you inline F# code.
Add-TypeFSharp -TypeDefinition @" module MyModule let Add a b = a + b;; "@ 1..10 | % { [MyModule]::Add( $_, $_*2 ) }
Things TODO
- Install .NET 4.0, VS 2010 installs this and the latest F# bits
- Download and install The F# PowerPack
- Update the registry so PowerShell can work with .NET 4.0
Updating the registry
This is a bit tricky, thanks to the help of the PowerShell MVP community (Shay, OisÃn, Marco and Vadims)on getting this to work.
Before you are allowed to edit this entry, you need to change the permissions on that key and take full ownership.
Then change the RuntimeVersion value, under the following key, to v4.0.30319.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine
Function Add-TypeFSharp
Function Add-TypeFSharp { param( [string]$TypeDefinition ) Add-Type -Path ` "C:\Program Files\FSharpPowerPack-1.9.9.9\bin\FSharp.Compiler.CodeDom.dll" $provider = New-Object ` Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider Add-Type -TypeDefinition $TypeDefinition -CodeDomProvider $provider }
Notes
- Investigate using a PowerShell Proxy command for Add-Type and extend the Language parameter
- Enhance Add-TypeFSharp to take an F# file in addition to a string



{ 4 trackbacks }
{ 3 comments… read them below or add one }
Very cool!!
If you don’t install VS 2010, and you want to use .net 4, you need .net 4 and the separate F# 2.0 runtime installer as well (it’s not yet fully integrated into the platform)
If you want to use lower versions of .net, then the April 2010 CTP for F# is what you want. Using the lower versions means you don’t have to play with the registry keys for PowerShell.
The code given still works just as well in my pre-4.0 environment.
Great point Steve, thanks for the clarification.