Run Powershell Silently via NSIS
Asked Answered
S

3

5

I have a powershell script that I want to run silently. I am using NSIS script, it's still promoting the powershell command prompt when .exe file is ran..

Is there a way so it will silently.

!include FileFunc.nsh
!include x64.nsh


OutFile "script.exe"
SilentInstall silent
RequestExecutionLevel admin

Function .onInit
    SetSilent silent
FunctionEnd

Section
    SetOutPath $EXEDIR
    File "script.ps1"
    IfSilent 0 +2
        ExecWait "powershell -ExecutionPolicy Bypass .\script.ps1 -FFFeatureOff"
SectionEnd

Function .onInstSuccess
    Delete "script.ps1"
FunctionEnd

There is an example here that uses silent install, but I couldn't get it working when I tried it. http://nsis.sourceforge.net/Examples/silent.nsi

Seismology answered 13/10, 2015 at 19:37 Comment(1)
Why are you using IfSilent, "SilentInstall silent" will force it to always be silent.Sickroom
S
6

Try this:

ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\script.ps1 -FFFeatureOff"

More info: PowerShell.exe Command-Line Help

Shielashield answered 13/10, 2015 at 19:42 Comment(2)
This will briefly display a console window on the screen because the console window is created before the powershell process is able to hide it.Sickroom
@Sickroom Yep, my bad, I should have checked this myself. Thanks for correction.Shielashield
S
4

Powershell.exe is a console application and console applications get a console window by default and the NSIS silent parameter has no impact on console windows created for child processes. A parameter like -WindowStyle Hidden that can be passed to the child process will always cause a console window to be visible on the screen for a short period of time because Windows will create the console window before the child process starts running.

If you need to hide a console window then you should use a plugin. nsExec is part of the default install or you could use a 3rd-party plugin like ExecDos that offers more advanced features like stdin handling.

If you don't need to wait for the child process then you can try ExecShell as suggested by Serge Z...

Sickroom answered 15/10, 2015 at 10:50 Comment(1)
NSIS now has ExecShellWait as well.Sickroom
M
3

You can try ExecShell for this, it allows to hide console via SW_HIDE flag:

ExpandEnvStrings $0 "%COMSPEC%"
ExecShell "" '"$0"' "/C powershell -ExecutionPolicy Bypass .\script.ps1 -FFFeatureOff" SW_HIDE

Also, refer to this question: Exec vs ExecWait vs ExecShell vs nsExec::Exec vs nsExec::ExecToLog vs nsExec::ExecToStack vs ExecDos vs ExeCmd

Maudiemaudlin answered 14/10, 2015 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.