How to run commands in cmd [elevated] using PowerShell? - powershell

I want to run elevated command, for example calc.exe in cmd using PowerShell. I've tried from PowerShell:
Start-Process -Verb RunAs cmd calc.exe
All it does is opening the elevated cmd, but does not run calc.exe (command).
What am I doing wrong? Can someone help me?

The executable invoked by Start-Process is cmd; you must specify its arguments separately, via the -ArgumentList (-Args) parameter, as an array (,-separated):
Start-Process -Verb RunAs cmd -Args /k, calc.exe
Note: The arguments happen not to need quoting here, but --prefixed arguments or arguments with embedded spaces do; quoting is always an option ('/k', 'calc.exe')
Note:
/k is required to keep the cmd console window open after launching calc.exe (/c would close it right after, but in that case you could just skip cmd and pass calc.exe directly to Start-Process).
Generally, you need either /c or /k in order to pass a command to execute to cmd; without that, an argument such as calc.exe is simply ignored.
Run cmd /? for more information.

Related

Powershell scriptopen a window and send command to window

I'm trying to open a CMD as system using PowerShell and then send a command to that system window.
what i've got so far is this:
$opensystempromt = '"/c PsExec.exe -is cmd.exe'
[System.Diagnostics.Process]::Start('cmd.exe',$opensystempromt)
$systemcommand = 'CustomerService.exe deploy aa.txt bb.txt'
what i can't figure out is how to send the system command to the newly opened CMD.
and there's no documentation for it anywhere, that i could find
I'd use Start-Process to open cmd.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k Insert your variables here}
with -verb runas you'll open cmd as an administrator. ArgumentList is your "command part". /k leaves the cmd open, after the commands are done. If you want cmd to close instead, use /c.

Supress PowerShell prompting "Press any key to continue..."

I'm running a powershell script from a .bat to make it double click friendly. Here's the code:
powershell.exe -ExecutionPolicy unrestricted -NoExit -command "Start-Process powershell -Verb RunAs" {<Location of .ps1 file here>; <arguments>; pause }
It runs fine but when it completes it says "Press any key to continue" and the powershell window closes but I need it to remain open because at the end of the script it displays some additional commands we need to run to complete the process.
Any ideas? Thanks!
To keep a PowerShell window open (its session alive) after executing a command with -Command (or -File), use the CLI's -NoExit parameter, as already shown in your question for the outer PowerShell call.
You then have no need for an interactive delay-the-closing command such as pause (which is a function that simply calls $null = Read-Host 'Press Enter to continue...').
Therefore:
Add a -NoExit parameter to the inner, Start-Process-launched PowerShell instance as well or - more likely - instead; you need to scroll to the right to see it in the command below.
Remove the pause command from the inner command string.
Note: I'm assuming that you don't actually need the outer -NoExit, as it would block the batch file indefinitely by entering an interactive PowerShell session, so it is omitted from the command below.
powershell.exe -ExecutionPolicy unrestricted -command "Start-Process powershell -Verb RunAs '-NoExit', '-Command', '...'"
Note that there's no point in using a script block ({ ... }) with Start-Process, because only strings are supported as arguments by Start-Process; hence, '...' is used above to represent the command string to pass to the inner PowerShell instance.

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd

Run powershell in new window

I would like to run new powershell window with parameters. I was trying to run following:
powershell -Command "get-date"
but everything happens in same console. Is there a simple way to do this?
To open a new PowerShell Window from PowerShell:
Start-Process PowerShell
Or my personal favorite:
Start-Process PowerShell -WindowStyle Maximized
Then you could typeGet-Datewithout having to deal with the -ArgumentList's tendency to close itself. I still haven't found a way to open a new PowerShell process with the -ArgumentList Parameter, without it immediately closing after it runs. For Instance:
Start-Process PowerShell -ArgumentList "Get-Date"
or simply
Start-Process PowerShell -ArgumentList Get-Date
Will Close Immediately after running the process.
In order to get it to wait before closing you could add:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Since the -Wait Parameter seems to do nothing at all in this case.
FYI - PowerShell Suggested Syntax is actually:
Start-Process -FilePath "powershell.exe"
But since PowerShell is a standard Windows Application in the %SystemRoot%\system32 Environment Variables the command line(s) should recognize a simple
Powershell
Command
Use the start command. In a CMD prompt, try:
start powershell -noexit -command "get-date"
For Start/Run (or Win+r) prompt, try:
cmd /c start powershell -noexit -command "get-date"
The -noexit will tell Powershell to, well, not to exit. If you omit this parameter, the command will be executed and you are likely to just see a glimpse of a Powershell window. For interactive use, this is a must. For scripts it is not needed.
Edit:
start is an internal command for CMD. In Powershell it is an alias for Start-Process. These are not the same thing.
As for why the window is black, that's because the shortcut for Powershell.exe is configured to set the background blue.
To call a PowerShell (PS) script in a second terminal window without exiting, you can use a script similar to:
Start-Process PowerShell -ArgumentList "-noexit", "get-date"
or if you need to run another script from a specific location:
Start-Process PowerShell -ArgumentList "-noexit", "-command .\local_path\start_server.ps1"

How to write "start /b /min FeedDemon.exe" in powershell

How would I write the following windows command in powershell?
start /b /min FeedDemon.exe
When I run that on the command line, it minimizes the window fine and works great in a startup script. But in powershell, I cannot get the same results when I try:
start-process -FilePath FeedDemon.exe -WindowStyle Minimized
I am still learning powershell (and Windows in general) so please be gentle.
The /b argument to start indicates that the program should run without popping a new console window. The direct translation to powershell would be
Start-Process FeedDemon.exe -NoNewWindow -WindowStyle Minimized
If that doesn't work, just call it the old fashioned way, from Powershell
cmd.exe /c start /b /min FeedDemon.exe