2

I have a bunch of batch files I need to call from a 32-bit command line.

I have this code:

$cmd = "C:\Windows\SysWOW64\cmd.exe"
$args="/C"
$pipe = "0 |"
Start-Process $cmd $args

If I only call $cmd it opens the command line window. The title says "C:\windows\syswow64\cmd.exe", but the path in the window says "C:\windows\system32".

How can I force PowerShell to run all the batch files to run in a 32-bit system? Also, how can I use the $pipe since some of the batch files require "press any key to continue"?

I was able to run a 32 bit commandline with the code below.

$CMD = "C:\Windows\SysWOW64\cmd.exe"
$test = 'C:\Update\test folder\test.cmd'
$proc = (Invoke-WmiMethod Win32_Process Create "$cmd /c $test")

However, because the folder name has spaces, the code above doesn't work. If I delete the space in the folder name, everything works.

I tried double quotes, single quotes, $a = $test.tostring() with no success. How can I fix this?

6
  • 3
    if you call cmd.exe from syswow64 it is the 32bit executable, the path that is displayed is the workingdirectory and has nothing to do with what executable you are using (unless you supply a workingdirectory it will use the directory your powershell is on at time of call). if you want to pipe something in you will have to do it in your script directly and not via a variable. Also Start-Process does not accept random pipeline input so will have to call the cmd in another fashion Commented Mar 17, 2016 at 14:21
  • Why do you need to use Start-Process at all? PowerShell can run a batch file if you specify its name. Commented Mar 17, 2016 at 17:50
  • Hi Bill, I need to be able to call the batch file within a 32-bit system. I'm having an issue with that. Commented Mar 18, 2016 at 12:17
  • & $Env:SystemRoot\SysWOW64\cmd.exe /c D:\Path\test.bat will run the batch file using the 32-bit copy of cmd.exe. But why do you need to do it? Commented Mar 18, 2016 at 20:25
  • Hi Bill, one of the batch files calls a software that has to run in 32 bit. It really is a long story :) Thank you for your help. Commented Mar 22, 2016 at 15:50

3 Answers 3

1

You are running the 32-bit executable. You can verify this for instance with Process Explorer by displaying the Image Type column (View → Show Columns → Process Image). The path shown in the CMD window is just the working directory and has nothing to do with whether the process is 32-bit or 64-bit.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Ansgar, I was able to confirm that it does run the cmd with 32 bit. However, now I'm having problems call the batch file. start-process $cmd $batch(path to the batch file) doesn't work. It just opens the command window. Any ideas?
What happens if you run Start-Process $cmd '/c "C:\path\to\your.cmd"', or just & $cmd /c "C:\path\to\your.cmd" (without Start-Process)?
Errors out with start-process "Start-Process : This command cannot be run due to the error: The system cannot find the file specified." without start-process nothing happens
Then you have an incorrect path in $cmd. What is the output of echo "-$cmd-" and Test-Path -LiteralPath $cmd?
0

I ended up finding the code below which forces it to call cmd in 32 bit.

$NewProcInfo = New-Object System.Diagnostics.ProcessStartInfo
$NewProcInfo.FileName = "$CMD"
$NewProcInfo.RedirectStandardError = $true
$NewProcInfo.RedirectStandardOutput = $true
$NewProcInfo.UseShellExecute = $false
$Path = "`"$batchfile`""
$args = @("/c", "$Path")
$NewProcInfo.Arguments = "$args"
$Proc = New-Object System.Diagnostics.Process
$Proc.StartInfo = $NewProcInfo
$Proc.Start() | Out-Null
$Proc.WaitForExit()
$ReturnCode = $Proc.ExitCode

3 Comments

& $Env:SystemRoot\SysWOW64\cmd.exe /c D:\Path\test.bat is a lot shorter. You also haven't said why you need to use the 32-bit copy of cmd.exe.
I tired that method. But when I checked with process explorer as Ansgar suggested and it was showing that the cmd was still running under x64. I answered why I need to call 32 bit under your comment above.
Not correct. $Env:SystemRoot\SysWOW64\cmd.exe is a 32-bit executable. I suspect you're looking at the wrong instance of cmd.exe.
0

You initial solutions worked. You just made the classic thinking error.

You start the 32-bit cmd.exe from C:\Windows\SysWOW64.

The DOS box will of course show your current directory C:\Windows\System32. It is a 32-bit application. C:\Windows\System32 in a 32-bit application on a 64-bit Windows is actually the directory C:\Windows\SysWOW64.

You are just locked inside the 32-bit environment inside the DOS box.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.