1

When I use this simple batch code it runs the PowerShell command immediately, it should be waiting for the input of number '1' before executing the command.

The code:

@echo off
:start
Echo 1. Choose to run CMD as admin
set number=
if '%number%'=='1' goto a
:a
powershell -Command "Start-Process cmd -Verb RunAs
goto end
:end
pause

2 Answers 2

2
set /P number=
if "%number%"=="1" goto a

The /p instructs "wait and assign input"

Double-quotes required to delimit strings

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

2 Comments

@Lieuwe To add on, I'd suggest using "Start-Process cmd -Verb runAs -NoNewWindow"
Despite the necessary change as answered by @Magoo, the powershell command will still run regardless of the value of %number%. This can be fixed easily by moving goto end to a new line above :a.
0

I would suggest something along these lines:

@Echo Off
Echo 1. Choose to run CMD as admin
Set/P "number="
If "%number%"=="1" GoTo a
GoTo end

:a
PowerShell -C "& {Start-Process Cmd -Verb RunAs -A "/K", "CD/D", "\"%~dp0""}"

:end
Pause

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.