Use import-module with a PowerShell PSSnapin Dll

1.3k Views Asked by At

We have a legacy PowerShell PsSnapin (c#). I want to avoid having to use InstallUtil. The following imports a module with the PSSnapin cmdlets exported:

import-module .\MySnapin.dll

However, when I run the module cmdlets, they fail due to not being able to find referenced assemblies (specifically, Enterprise Library dlls).

Is there a neat way to get this working?

(The PsSnapin dll and all referenced assemblies are in the same build directory, and when I use installutil, the dependencies are all resolved correctly)

1

There are 1 best solutions below

2
Mathias R. Jessen On

Snap-ins are a bit different from Modules. You first need to register the snap-in, using InstallUtil.exe:

PS> $InstallUtil = Join-Path $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) "InstallUtil.exe"
PS> & $InstallUtil "C:\Path\to\MySnapin.dll"

After registering the snapin assembly, you can load it into your powershell session with Add-PSSnapin:

PS> Add-PSSnapIn MySnapin