How to open an elevated cmd using command line for Windows? – Stack Overflow

How to open an elevated cmd using command line for windows?

Similar to some of the other solutions above, I created an elevate batch file which runs an elevated PowerShell window, bypassing the execution policy to enable running everything from simple commands to batch files to complex PowerShell scripts. I recommend sticking it in your C:WindowsSystem32 folder for ease of use.

The original elevate command executes its task, captures the output, closes the spawned PowerShell window and then returns, writing out the captured output to the original window.

I created two variants, elevatep and elevatex, which respectively pause and keep the PowerShell window open for more work.

And in case my link ever dies, here’s the code for the original elevate batch file:

@Echo Off
REM Executes a command in an elevated PowerShell window and captures/displays output
REM Note that any file paths must be fully qualified!

REM Example: elevate myAdminCommand -myArg1 -myArg2 someValue

if "%1"=="" (
    REM If no command is passed, simply open an elevated PowerShell window.
    PowerShell -Command "& {Start-Process PowerShell.exe -Wait -Verb RunAs}"
) ELSE (
    REM Copy command arguments (passed as a parameter) into a ps1 file
    REM Start PowerShell with Elevated access (prompting UAC confirmation)
    REM     and run the ps1 file
    REM     then close elevated window when finished
    REM Output captured results

    IF EXIST %temp%trans.txt del %temp%trans.txt
    Echo %* ^> %temp%trans.txt *^>^&1 > %temp%tmp.ps1
    Echo $error[0] ^| Add-Content %temp%trans.txt -Encoding Default >> %temp%tmp.ps1
    PowerShell -Command "& {Start-Process PowerShell.exe -Wait -ArgumentList '-ExecutionPolicy Bypass -File ""%temp%tmp.ps1""' -Verb RunAs}"
    Type %temp%trans.txt

How to start an application without waiting in a batch file?

I’m making a guess here, but your start invocation probably looks like this:

start "FooBarPath with spaces in itprogram.exe"

This will open a new console window, using “FooBarPath with spaces in itprogram.exe” as its title.

:/>  How to Rename User and User Folder in Windows 7, 8, 8.1 or 10 OS. • Repair Windows™

If you use start with something that is (or needs to be) surrounded by quotes, you need to put empty quotes as the first argument:

start "" "FooBarPath with spaces in itprogram.exe"

This is because start interprets the first quoted argument it finds as the window title for a new console window.

How to append content to an existing log file without overwriting using windows command line?

I have a batch file where I would like to append to the same log file, without overwriting, from two or more build files. I intend to write something like below:

SET logdir=C:LogsBuildLogFile.txt
NAnt > %logdir% 

SET logdir=C:LogsBuildLogFile.txt
NAnt > %logdir% 

But the txt file gets overwritten. Is there any way by which I can append to the same .txt file because I want to use a single Log file for the entire project?

Оставьте комментарий