- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
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.
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 184.108.40.206 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.
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.
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.