F# friendly PowerShell extension
(net45 and Windows PowerShell)
The NuGet package FarNet.FSharp.PowerShell may be used as usual in F# projects.
Note, System.Management.Automation.dll is not needed in your final binaries.
The package is also designed for FarNet.FSharpFar.
To install FarNet packages, follow these steps.
F# code
The PS
type wraps the PowerShell class and exposes somewhat similar members.
Use PS.Create()
instead of PowerShell.Create()
.
Use Script()
and Command()
instead of AddScript
and AddCommand()
. PS
does not directly support command chains. But it is fine to invoke several scripts and commands using the same PS
instance.
Use the type safe generic InvokeAs()
in addition to Invoke()
. Result objects must be compatible with the specified type.
Use F# asynchronous InvokeAsync()
and InvokeAsyncAs()
. For parallel scenarios use different PS
instances.
Use the operator ?
for getting PSObject
properties. Note, it also works for Hashtable
wrapped by PSObject
.
PowerShell code
The default $ErrorActionPreference
is Stop
, safe for non-interactive.
Use Get-Type
to get a type defined in the calling F# assembly or script.
"Hello, world!" example:
open FarNet.FSharp.PowerShell
PS.Create().Script("Write-Output 'Hello, world!'").Invoke()
|> printfn "%A"
For more examples see /samples.
Features and API may change before v1.0.
The project is suitable for cloning and playing with Visual Studio.
FarNet.FSharp.PowerShell.sln contains the main project and tests.
FSharpFar development and tools are optional. They require:
C:\Bin\Far\x64