running a cmd within powershell

Hippopotamus Defence 116 Reputation points
2021-06-07T20:54:51.92+00:00

I am trying to run a cmd file through powershell. The file is located in a folder inside Program Files.

I have tried

invoke-item -path "c:\program files\myProgram\myfile.cmd" which gave me cmd file not found message.

Then, I tried to put that in variable and pipe it to cmd
$myvariable = "c:\program files\myProgram\myfile.cmd"
$myvariable | cmd

That gave an error "'C:\Program' is not recognized as an internal or external command". Apparently, the space in the string there is a problem.

Next, I tried to cd to "c:\program files\myProgram" and then run cmd /c myfile.cmd. Now that seems to work except the powershell does not stop and has to be manually stopped.

Any suggestions as to how to run that cmd from within powershell script? When run from command prompt (elevated privilege) it works fine.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-06-07T23:22:04.577+00:00

    Just run cmd.exe and put quotes around the cmd file name.

    103127-capture.jpg

    1 person found this answer helpful.

  2. Balaji 51 Reputation points
    2023-01-27T11:20:45.8233333+00:00

    cmd ok

    Hi Sir,

    I am trying to install the Docker Desktop in the VM using CMD in powershell.

    It's asking for press OK but it's manual process manually click on OK.

    So can you please help to give Auto Answer OK/Yes to install Docker Desktop without manual intervention?

    PFB reference article for commands: [https://docs.docker.com/desktop/install/windows-install/


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.