How to run PowerShell script without terminal window? - powershell

The shortcut below executes powershell.exe and passes it the script vscode.ps1. It works except that for all my attempts, it still displays a terminal window briefly when running. You can see I've passed parameters which is supposed to prevent this, but I still see the window. What do I have to do to execute a PS script from a shortcut without the terminal window being displayed?
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -WindowStyle Hidden -file C:\Users\bernie\OneDrive\PowerShell\vscode.ps1 C:\Users\bernie\OneDrive\Documents\windows.txt

It's a known issue, see https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
As #Ciantic mentioned, the best way to work around this issue is by using a VB script:
In, say ps-run.vbs put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
Then use it to run the command you want, e.g.
wscript "C:\Path\To\ps-run.vbs" "C:\Other\Path\To\your-script.ps1"
I use something like this to run a task frequently without seeing any flashing windows.

Yes, Powershell can't completely do this. But VBScript can do this. Also I use Hidden Start which can start any program hidden or bypass UAC.

Related

Powershell window does not hide even after adding -WindowsStyle hidden while running task from task scheduler

I have created a task in task scheduler.
Its basically a powershell script which has to run in interactive mode.
Task is running under SYSTEM account.
In Actions tab, under Program/Script I have added path of the ServiceUI.exe e.g, C:**\ServiceUI.exe
Under Add arguments option, I have added C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -Windowstyle hidden -NoProfile -Executionpolicy bypass -file "C:**\PS1.ps1"
When I run the task, powershell window prompt shows for a fraction of a second.
Could someone please suggest a way to hide it?
I was able to fix the problem by using vb script.
WScript.Quit CreateObject("WScript.Shell").Run("powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File " & PSscriptPath, 0, true)
In Actions tab, under Program/Script I had path of the ServiceUI.exe e.g, C:**\ServiceUI.exe
Added wscript.exe "vbs script path" in the arguments option.
Powershell prompt doesn't come up, and task works perfectly in interactive mode under SYSTEM account using these options.

How to run a PowerShell Script in the background (no window) from Windows Explorer? [duplicate]

The shortcut below executes powershell.exe and passes it the script vscode.ps1. It works except that for all my attempts, it still displays a terminal window briefly when running. You can see I've passed parameters which is supposed to prevent this, but I still see the window. What do I have to do to execute a PS script from a shortcut without the terminal window being displayed?
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -WindowStyle Hidden -file C:\Users\bernie\OneDrive\PowerShell\vscode.ps1 C:\Users\bernie\OneDrive\Documents\windows.txt
It's a known issue, see https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
As #Ciantic mentioned, the best way to work around this issue is by using a VB script:
In, say ps-run.vbs put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
Then use it to run the command you want, e.g.
wscript "C:\Path\To\ps-run.vbs" "C:\Other\Path\To\your-script.ps1"
I use something like this to run a task frequently without seeing any flashing windows.
Yes, Powershell can't completely do this. But VBScript can do this. Also I use Hidden Start which can start any program hidden or bypass UAC.

Windows Sandbox PowerShell logon command window not visible

I'm trying to use Windows Sandbox with a PowerShell logon command. This is the LogonCommand section of my WSB file:
<LogonCommand>
<Command>C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -executionpolicy unrestricted -file "C:\\Users\\WDAGUtilityAccount\\Desktop\\boot.ps1" -noexit</Command>
</LogonCommand>
The Windows Sandbox instance loads up okay suggesting no syntactic/validation issues with the WSB file content, but the PowerShell window is not shown. Adding -windowstyle normal has no effect.
I suspect the LogonCommand content is run in a command prompt which is not made visible so running the command to open PowerShell from it somehow "inherits" the terminal window not being visible.
Is it possible to force the PowerShell terminal window to reveal itself in such a case? I want to do this so that I can see the errors that I get because the PowerShell script is not executing as expected and I'm blind to any output/progress indication.
Found an answer (doesn't look like the cleanest option, but works):
<Command>powershell -executionpolicy unrestricted -command "start powershell {-noexit -file C:\Users\WDAGUtilityAccount\Desktop\boot.ps1}"</Command>
powershell switches from CMD to PowerShell
-windowstyle normal won't work to make this PowerShell window visible
-executionpolicy unrestricted allows the nested PowerShell to run from file
start powershell runs another PowerShell with visible window
Running this directly for LogonCommand will not work
-noexit tells the nested PowerShell to remain visible
This is not necessary but it is useful for debugging the script errors
-file C:\Users\WDAGUtilityAccount\Desktop\boot.ps1 runs the given script
Share it with the machine by using a MappedFolder in the WSB configuration

How to run a PowerShell script in folder having space in its name?

Running a script in C:\Users\Ooker\Desktop is fine, but yields error in C:\Users\Ooker\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.
I guess it's because of Start Menu have space in between, but I don't know how to double click the file and make it run. There seems to have no way to bracket the path beforehand.
I can run it in CLI, and it doesn't reference itself.
Few ways to do it.
Registry:
Edit your reg key at
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command
to
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"
or
Shortcut: Create a shortcut with the target:
powershell.exe -command "& 'C:\Users\Ooker\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\MyScript.ps1'"
keep in mind that the opening in a notepad is a safety measure in the event you accidentally run it when it is not intended.

Running PowerShell code inside VBS with many nested quotes, in one line

I'm starting to see double here from staring at this so long. I am trying to use an environment variable inside a script path that I'm going to launch from PowerShell, all initiated from a single line inside an mshta command run from a scheduled task on logon.
In case you were wondering, MSHTA can execute HTML/VBS/JS as if a local GUI app.
mshta vbscript:Execute("CreateObject(""Wscript.Shell"").Run ""powershell -NoLogo -Command """"& '\\$env:USERDNSDOMAIN\FOLDER1\FOLDER with Spaces\Folder3\Script-To-Run.ps1'"""""", 0 : window.close")
Premise: I have to do it this way to prevent momentary popups that flash onscreen when running from a scheduled task in user context. I cannot run this script in the System context.
I know that putting anything inside two single quotes gives me a string literal but if I try double quotes it seems to then use the space in the path. I've tried separating the two and concatenating them in all sorts of ways to no avail.
VBS equivalent of $env:USERDNSDOMAIN is
CreateObject("Wscript.Shell").ExpandEnvironmentStrings( "%USERDNSDOMAIN%" )
If the goal/purpose is to execute a PowerShell script without displaying the powershell.exe console window, you can do this by running it from a VBScript script executed by wscript.exe (not cscript.exe).
Example VBScript script:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -File """d:\path name\whatever.ps1"" [parameter [...]]", 0, False
The second parameter of the WshShell object's Run method is 0, so powershell.exe executes from a hidden window.
(Hint: The -File parameter to powershell.exe is generally easier to use than -Command. It must be the last parameter on the powershell.exe command line.)
End result: VBScript script runs the PowerShell script from a hidden window. Also, the VBScript itself has no console window, because you executed it using wscript.exe (the GUI host).