Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:04:35+00:00 2026-06-06T11:04:35+00:00

Im trying to execute a .bat file remotely in a powershell script. The code

  • 0

Im trying to execute a .bat file remotely in a powershell script. The code in my script file looks something like this:

$server
$cred
# Both of these methods are strings pointing to the location of the scripts on the
# remote server ie. C:\Scripts\remoteMethod.cmd
$method1
$method2
$scriptArg

Enter-PSSession $server -Credential $cred
Invoke-Command {&$method1}
if($?)
{
    Invoke-Command {&$method2 $args} -ArgumentList $scriptArg
}
Exit-PSSession

But whenever I run it I get

The term ‘C:\Scripts\remoteMethod.cmd’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

This happens even if i switch around the order the methods are called in.

I found this question Using Powershell's Invoke-Command to call a batch file with arguments and have tried the accepted answers to no avail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:04:38+00:00Added an answer on June 6, 2026 at 11:04 am

    I wouldn’t have expected the $method1 and $method2 variables to be available inside the scriptblock for Invoke-Command. Usually you would have to pass those in via the -ArgumentList parameter e.g.:

    Invoke-Command {param($method, $arg) &$method $arg} -Arg $method2 $scriptArg
    

    But it seems in your case the path to the CMD file is making it across the remote connection. Try this to see if the problem is related to a modified ComSpec environment variable on the remote machine:

    Invoke-Command {param($method, $arg) cmd.exe /c $method $arg} -Arg $method2, $scriptArg
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying in a powershell psake script to execute a .bat file. Is
Trying to execute powershell script in my C# code. Using .NET Framework v. 4.0.30319
I am trying to execute a .bat file remotely and implementing following lines of
I'm trying to create a .bat file to execute a flash video on a
I'm trying to execute a PowerShell script from a c# application. The script has
i am trying to execute the xxx.bat file for rename the file from my
I'm trying to execute a .bat script in a C# windows service but it
Im am trying to get MSDeploy to execute a PowerShell script on a remote
I am trying to execute a .bat to launch notepad.exe on my localhost(server-side) with
Im trying to execute this code on a mac. I have installed the curl.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.