Home > Enterprise >  Run CMD minimized from powershell
Run CMD minimized from powershell

Time:02-26

I have a script that at some point opens a cmd window

this is the part of the script that does that

$cmd = "C:\WINDOWS\system32\cmd.exe"

 Copy-Item $cmd -Destination $newPath -Force

 Write-Host "$cmd copied as $newPath."

 $null = New-ItemProperty -Path "HKCU:\Environment" -Name $enviromentVariableName -Value $secondaryPath -Force

 Write-Host "Value of $enviromentVariableName changed to $secondaryPath"

 Write-Host "Ejecutando $taskName"

 $null = "schtasks /Run /TN $taskPath /I" | IEX

However I have no idea how (if possible) to open this window minimized

CodePudding user response:

I'm assuming you're looking to minimize the console window that appears when you run your scheduled task on demand with schtasks /Run:

Minimizing such a window does not appear to be possible using the features of scheduled tasks alone.

You have two options:

If it is OK to run the task invisibly:

Configure the task with the Run whether user is logged on or not in the Task Scheduler GUI (taskschd.msc) (instead of Run only when user is logged on)

Caveat:

  • This requires you to specify the user's password, which is saved with the task, and apparently breaks the task definition the next time the user's password changes.

  • Configuring the task to run as the SYSTEM account avoids this problem, but may be problematic from a security perspective (the account is highly privilege) - also, on-demand running of the task then requires elevation (running as admin).


If you do need the task's window to be visible, but initially minimized:

This requires you to modify the task's command by wrapping it in a command that explicitly launches the original command in a minimized window.

This wrapper command must itself be a GUI-subsystem application, so that it doesn't itself causes a non-minimized window to appear (at least briefly).

You can create a helper VBScript that you can launch via the GUI version of the Windows Scripting Host, wscript.exe.

If you create helper file runMinimized.vbs as defined below in, say, c:\foo and your original command is, say, bar.exe baz 42 (executable bar.exe with arguments list baz 42), you'd redefine your task action as follows:

  • Executable (field Program/script: in the Task Scheduler GUI):

    • wscript.exe
  • Arguments (field Add arguments (optional): in the Task Scheduler GUI):

    • c:\foo\runMinimized.vbs bar.exe baz 42

runMinimzed.vbs source code:

' Separate the arguments into the executable name
' and a single string containing all arguments.
exe = WScript.Arguments(0)
sep = ""
for i = 1 to WScript.Arguments.Count -1
  ' Enclose arguments in "..." to preserve their original partitioning, if necessary.
  if Instr(WScript.Arguments(i), " ") > 0 then
    args = args & sep & """" & WScript.Arguments(i) & """"
  else
    args = args & sep & WScript.Arguments(i)
  end if
  sep = " "
next

' Execute the command with its window *minimized*, without stealing focus (7)
WScript.CreateObject("Shell.Application").ShellExecute exe, args, "", "open", 7

Note: To alternatively run the window invisibly (hidden), name the script runHidden.vbs instead, and substitute 0 for 7 in the .ShellExecute() call above.

CodePudding user response:

With both SHIFT and CTRL held down, drag and drop whatever it is you want - creating a shortcut. Right click on the shortcut and select properties, go to the "Shortcut" tab, change "Run:" to "Minimized", and press the OK button.

Now use PowerShell to execute the shortcut, and the shortcut will in turn execute your desired item in a minimized state.

  • Related