r/PowerShell • u/aq-modeler • Feb 09 '26
Executing batch scripts on remote computer
I normally copy and paste batch files from my local computer to a remote computer in my office using Remote Desktop Connection and then double-click those batch files on the remote computer to run them. I want to add a script to copy and then execute these files and add it to the right-click menu (which I know how to do).
I have a batch script that uses robocopy to copy the batch files that are drug onto the batch file in Windows Explorer to the remote computer. This works great. I then have that batch file execute a PowerShell script, see below:
set destination="W:\Users\My Name\Desktop\Name AERMOD-1"
for %%F in (%*) do (
robocopy "%%~dpF." %destination% "%%~nxF"
)
powershell.exe -NoProfile -ExecutionPolicy Bypass -File ".\model1.ps1"Here is the PowerShell script:
$cred = Import-Clixml C:\PSFolder\mycredentials.xml
$computerName = "192.168.0.10"
$files = "C:\Users\My Name\Desktop\Name AERMOD-1\*.bat"
$parameters = @{
ComputerName = $computerName
Credential = $cred
ScriptBlock = { cmd.exe /c $files }
}
Invoke-Command u/parametersIt does not show any error messages, but the batch files don't appear to run. If they did run, command windows should open up on the remote computer. Also, the normal output files are not being created by those batch files.
I have also run this interactively with the same result. Can anyone help me please? I just want those batch files on the remote machine to be executed, nothing needs to happen on the local computer.
View all comments
Show parent comment
1
u/omglazrgunpewpew Feb 11 '26 edited Feb 11 '26
I just made the connection to your username and AERMOD reference in your post. Guessing you're running an AERMOD swarm?
The extra context you gave changes everything. Your current method fails because standard PS Remoting (
Invoke-Command) runs in Session 0. This is a "non-interactive" background session. Windows strictly forbids background services from popping up windows on your desktop (Session 1+) for security reasons. That is why the scripts run (you'd see them in Task Manager), but the windows remain invisible.I think the best solution for you is to leverage PsExec. You have to "break" the session boundary to force those windows to appear on the visible desktop. You can run this loop from your local machine. The
-i(Interactive) is the magic switch that makes the remote window appear:Throw the
-dflag in to help with parallel processing. It tells PsExec to launch the process and immediately disconnect, so your loop can fire off all 32 instances in seconds without waiting for the first one to finish.Also worth noting launching 32 command prompts instantly might trigger a temp alert on some AV software (looks like a fork bomb), so you miiiight need to add an exclusion for your modeling folder.
Prereqs to make PsExec work:
PsExec.exein your script folder.ADMIN$share to be open (File and Printer Sharing) on the remote PCs. Since you're already copying files there, prob already set.-accepteulaswitch to your command to prevent it from hanging your script.