I'm trying an experiment and I want to create a CmdLet for PowerShell in NET8. I have created a new Class Library and added the following packages:
- Microsoft.PowerShell.SDK
- PowerShellStandard.Library
as suggested in this post. Then, I added this code to create the CmdLet:
using System.Management.Automation;
namespace PowerShellCmdLet
{
[Cmdlet("Get", "Test")]
public class TestClass : Cmdlet
{
protected override void BeginProcessing()
{
Console.WriteLine("BeginProcessing");
}
}
}
When I open PowerShell on the project bin directory, I import my dll with
Import-Module .\PowerShellCmdLet.dll
but then I get this error:
Import-Module : Could not load file or assembly 'System.Management.Automation, Version=7.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. At line:1 char:1 Import-Module .\PowerShellCmdLet.dll
CategoryInfo : NotSpecified: (:) [Import-Module], FileNotFoundException FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
I tried to open PowerShell as an administrator but I get the same issue.
powershell.exe, notpwsh.exe?powershell.exeonly hosts Windows PowerShell 5.1, which is built on .NET Framework. It'll never be able to load your .NET 8 assembly.pwsh.exeare you running?"$($PSVersionTable.PSVersion)"