powershellpowershell-remotingwinrmpowershell-corewsman

PowerShell remoting: Controlling what edition is being targeted (PowerShell (Core) 7 or Windows PowerShell); the state of cross-platform remoting


This self-answered question, which focuses on Windows[1], addresses the following aspects:

Now that there are two PowerShell editions - the legacy, Windows-only Windows PowerShell and the cross-platform PowerShell (Core) 7, both may be installed on a given Windows machine:

Note:

For an edition to be targetable via remoting on a given machine, it must be set up for remoting:

In any event, you can use Enable-PSRemoting to (re-)enable PowerShell remoting on demand, which:


[1] That is, the question focuses on WinRM-based remoting (WinRM is a Windows-specific implementation of the DTMF WSMan (WS-Management) standard).

As for cross-platform remoting with PowerShell (Core) 7:


Solution

  • Note: Changing what remote endpoint PowerShell [Core] targets by default - which as of 7.0 is still Window PowerShell - is being considered: see GitHub issue #11616.


    It is the locally specified remoting session configuration that determines what PowerShell edition, and possibly version, will be used on the remote machine:

    On the target machine of a remoting operation, Get-PSSessionConfiguration cmdlet lists all registered session configurations that clients can use to connect to, and which you can manage with Register-PSSessionConfiguration and Unregister-PSSessionConfiguration:

    $PSSessionConfigurationName defaults to 'http://schemas.microsoft.com/powershell/Microsoft.PowerShell' in both editions, which means that Windows PowerShell is by default targeted on remote machines even if you're running from PowerShell Core:

    To target PowerShell (Core) 7 on a remote machine:

    # Connect to computer $comp and make it execute $PSVersionTable 
    # in PowerShell Core v7.x, which tells you what PowerShell edition 
    # and version is running.
    Invoke-Command -ComputerName $comp -ConfigurationName PowerShell.7 { $PSVersionTable }
    
    # When remoting, default to running PowerShell (Core) v7.x on the
    # the target machines:
    $PSSessionConfigurationName = 'PowerShell.7'
    
    # Run WITH ELEVATION (as administrator) and
    # ONLY IF YOU UNDERSTAND THE IMPLICATIONS.
    
    $ErrorActionPreference = 'Stop'
    
    # The configuration whose definition you want to make the new default.
    $newDefaultConfigSource = 'PowerShell.7'
    
    # Standard registry locations and names.
    $defaultConfigName = 'Microsoft.PowerShell'
    $configXmlValueName = 'ConfigXml'
    $configRootKey = 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Plugin'
    
    # Rename the current default configuration XML to "ConfigXml.OLD" to keep a backup.
    Rename-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName -NewName "$configXmlValueName.OLD"
    
    # Get the configuration XML from the configuration that should become the new default.
    # Modify it to replace the source configuration name with the default configuration name.
    $xmlText = (Get-ItemPropertyValue $configRootKey\$newDefaultConfigSource $configXmlValueName) -replace 
                 ('\b{0}\b' -f [regex]::Escape($newDefaultConfigSource)), $defaultConfigName
    
    # Save the modified XML as the default configuration's config XML.
    Set-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName $xmlText
    
    # Restart the WinRM service for changes to take effect.
    Restart-Service WinRM