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:

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.

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.

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+

Users who have LIKED this post:

  • avatar
Share
3 Comments
  1. John 12 months ago

    Check if the winrm service is running on your localhost:
    PS C:>  Get-Service winrm | ft -AutoSizeStatus  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.

    1+

  2. Ian 3 weeks 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'.

    0

  3. Matt 2 weeks 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

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

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