4

Is there a way to run a powershell command when opening the run box (Windows+R)?

I'd like to just start typing get-process for example and have it execute that command in powershell and display the result (basically opening it and not closing it).

I know you can type "powershell /noexit get-process" to achieve this, but I'd like to know if there's a way the Run box realizes it's a powershell command and all I have to type is the command: get-process.

2 Answers 2

6

Might be a bit too late, but you are able to run CMD commands through the run box, and then again PowerShell commands through CMD. By utilizing this you can type:

cmd /k PowerShell.exe "Your PowerShell command"

where the /k can be changed wirh /c if you don't want the CMD-window to stay open. In the end it could look something like this

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

Comments

4

No, there is no way to make Windows+R understand PowerShell commands directly.

One workaround is to create a batch file with content like this:

@echo off
%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -noprofile -noLogo "%1 %2 %3 %4 %5 %6 %7 %8"

name it p.cmd and put it in a location that is in your path.

Now you can do:

Run example

start with p then the PowerShell command you want to execute.

2 Comments

Yes, Powershell.exe is in the path, but using the fully qualified path to it means Windows doesn't have to look through all the locations in the %path% to find it. It may only save a few milliseconds, but that's the way I do it. Plus what if someone adds a different powershell.exe to a location that is first listed in %path% This can be a security issue.
A tiny improvement: use %* instead of %1 %2 %3... that will allow you to pass as many arguments as you want.

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.