The WindowsCompatibility module allows you to run modules that only work in Windows PowerShell within PowerShell Core using a few clever tricks.

PowerShell Core is the latest iteration of the popular Windows PowerShell. PowerShell Core not only works on Windows these days but also Linux and macOS too. However, due to this cross-platform push, many scripters haven't made the switch yet to PowerShell Core because many of the modules simply do not exist in PowerShell Core.

For example, perhaps I've installed PowerShell Core on my Windows machine and want to find events from the Windows event log. I type Get-EventLog expecting the normal output and get this instead:

get-eventlog : The term 'get-eventlog' is not recognized as the name of a cmdlet, function, script file, or operable program.

Commands not working in PowerShell Core

Commands not working in PowerShell Core

I could go back to Windows PowerShell, but I also could make this work in PowerShell Core using the WindowsCompatibility module. You can install the WindowsCompatibility module from the PowerShell Gallery:

Install-Module -Name WindowsCompatibility -Scope CurrentUser

Once you have the module installed, you now have the ability to use Windows PowerShell modules within PowerShell Core!

Now that I have the WindowsCompatibility module installed, let's try to use Get-EventLog again. This time, however, I have the opportunity to import the module that Get-EventLog is a part of not by using the typical Import-Module but instead Import-WinModule.

Import-WinModule is a command within the WindowsCompatibility module that uses implicit remoting either locally or remotely to make Windows PowerShell commands available within PowerShell Core.

I'll first need to figure out which module the Get-EventLog command is in. To do this, I'll hop over to a Windows PowerShell instance and find out. You'll see that Get-EventLog is in the Microsoft.PowerShell.Management module. I'll need to import this one into PowerShell Core.

PS C:\> Get-Command -Name Get-EventLog

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-EventLog                                       3.1.0.0    Microsoft.PowerShell.Management

Now that I know the module name, I can use Import-WinModule within PowerShell Core to create a proxy to all commands within the Microsoft.PowerShell.Management module that are not available in PowerShell Core.

Import-WinModule Microsoft.PowerShell.Management

After loading the module, I can try Get-EventLog to find that it's now working as if I were in Windows PowerShell!

The Import-WinModule command will be the most used command within the WindowsCompability module, but you've also got some other useful commands as well. Perhaps you've imported lots of Windows PowerShell modules within PowerShell Core. Instead of Get-Module, use Get-WinModule. When you do so, you'll probably find a lot more modules than what you expected to find.

Just like Windows PowerShell module behavior, Get-WinModule finds all modules it can in the Windows PowerShell module path and returns them. Get-WinModule will return all modules available that are importable via Import-WinModule. Get-WinModule uses a proxy session and finds all Windows PowerShell modules available and not just imported ones.

Since many commands within the WindowsCompatibility module create a "compatibility session," you can use this session not only implicitly through running Windows PowerShell commands but also explicitly by invoking commands directly in that session. Using the Invoke-WinSession command allows you to run any command with Windows PowerShell within PowerShell Core.

Using Invoke WinSession

Using Invoke WinSession

A few other useful commands within the WindowsCompatibility module will allow you to work with WindowsPowerShell from within PowerShell Core. The WindowsCompatibility module is open source and available on GitHub where you'll find command references, documentation, and more.

avatar
3 Comments
  1. John 4 years ago

    Check if the winrm service is running on your localhost:
    PS C:> Get-Service winrm | ft -AutoSize

    Status Name DisplayName
    ------ ---- -----------
    Running winrm Windows Remote Management (WS-Management)

    Otherwise PS remoting won’t work, though you’ve configured via winrm and have enabled PS remoting via Enable-PSRemoting.

  2. Ian 3 years ago

    Import-WinModule Microsoft.PowerShell.Management

     

    fails for me with the mysterious message:

    Method invocation failed because [System.Management.Automation.PSModuleInfo] does not contain a method named 'Where'.

  3. Matt 3 years ago

    Ian, I just ran into this problem myself.  My problem was that my Powershell version was 4.0.  When I upgraded to 5.1, Import-WinModule started working.

    Hope this helps,

    Matt

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account