How to add an F# type to a PowerShell session

April 18, 2010

in .NET 4.0,Add-Type,C#,F#,JScript,PowerShell,Visual Basic

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 ) }

 image

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 }

Tweets that mention How to add an F# type to a PowerShell session -- Topsy.com
04.18.10 at 3:37 pm
Rick Minerich's Development Wonderland
05.02.10 at 4:43 pm
How To Load .NET Assemblies In A PowerShell Session
08.29.10 at 12:04 pm
Using the VS2010 Publishing Configuration Transformations Outside of VS2010 | Matthew Bonig's Ramblings
01.11.12 at 10:24 pm

{ 3 comments… read them below or add one }

Anthony 04.19.10 at 2:29 pm

Very cool!!

Steve Gilham 04.23.10 at 11:17 am

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.

Doug Finke 04.23.10 at 1:34 pm

Great point Steve, thanks for the clarification.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2012, Doug Finke