1

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

Imsa
  • 881
  • 1
  • 13
  • 35

3 Answers3

4

Try this:

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

More info: PowerShell.exe Command-Line Help

beatcracker
  • 5,773
  • 1
  • 15
  • 36
  • 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. – Anders Oct 15 '15 at 10:54
  • @Anders Yep, my bad, I should have checked this myself. Thanks for correction. – beatcracker Oct 15 '15 at 11:53
2

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

Community
  • 1
  • 1
Serge Z
  • 385
  • 1
  • 11
2

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...

Community
  • 1
  • 1
Anders
  • 83,372
  • 11
  • 96
  • 148