- 1 year ago
Run Powershell Core from Powershell 5 remote pc
To run PowerShell Core from a PowerShell 5 remote session, you can use the Invoke-Command
cmdlet to execute commands on the remote PC. Here's an example:
powershell$session = New-PSSession -ComputerName <RemoteComputerName>
Invoke-Command -Session $session -ScriptBlock {
pwsh -Command "Write-Host 'Running PowerShell Core'"
}
Remove-PSSession -Session $session
Replace <RemoteComputerName>
with the name or IP address of the remote computer.
In the example above, we first establish a remote PowerShell session using the New-PSSession
cmdlet. Then, we use the Invoke-Command
cmdlet to execute a script block on the remote PC. Inside the script block, we use the pwsh
command to run PowerShell Core and execute a command (Write-Host 'Running PowerShell Core'
in this case).
Finally, we remove the remote PowerShell session using the Remove-PSSession
cmdlet.
By running the script, you should be able to execute PowerShell Core commands on the remote PC from your local PowerShell 5 session.
Note that both the local and remote PCs must have PowerShell Core installed for this to work. Ensure that PowerShell Core is properly installed on the remote PC before attempting the remote execution.