- 1 year ago
Execute CMD in remote desktop and receive output
To execute a CMD command on a remote desktop and receive the output using PowerShell, you can use the Invoke-Command
cmdlet. Here's an example:
powershell$computerName = "RemoteComputerName"
$command = "YourCommandHere"
$output = Invoke-Command -ComputerName $computerName -ScriptBlock { param($cmd) & cmd.exe /c $cmd } -ArgumentList $command
Write-Host "Command output:"
Write-Host $output
In this example, replace "RemoteComputerName"
with the name or IP address of the remote computer where you want to execute the command. Replace "YourCommandHere"
with the actual command you want to run on the remote computer.
The Invoke-Command
cmdlet establishes a remote PowerShell session to the specified computer using the -ComputerName
parameter. Within the remote session, it runs the specified command by invoking cmd.exe /c
with the provided command as an argument.
The output of the command is captured in the $output
variable, and then it is displayed on the console using Write-Host
. You can further process or manipulate the output as needed.
Please note that you need to have the necessary permissions and network access to the remote computer in order to execute commands remotely. Additionally, you may need to enable PowerShell remoting and configure the necessary firewall rules on the remote computer to allow incoming remote connections.
Ensure that you exercise caution when executing commands on remote computers and follow security best practices to protect your systems.