How can I run a PowerShell script without showing the terminal window?
Asked by
42plus17
I'm looking to execute a PowerShell script (.ps1) without having the terminal window flash on screen at all. I've tried the -WindowStyle Hidden option, but it still briefly shows the terminal. I want to avoid minimizing the window or utilizing external tools that might pose a security risk. The only effective method I found so far is using VBScript to hide the console, but since VBScript is deprecated by Microsoft, I'm keen on finding an alternative approach. Any suggestions?
Answers
You can try running your script with `conhost.exe --headless` to suppress the terminal window. Just be cautious, as some Endpoint Detection and Response (EDR) tools might flag this. Here's the command you can use: `conhost.exe --headless powershell.exe -File C:Scriptsmyscript.ps1`. It works well for me!
Reply to this answer
If it helps, converting the script to an executable (.exe) is another option to completely suppress the terminal. You can do this using the ps2exe module. Hereโs how: 1. Install the module with `Install-Module -Name ps2exe -Scope CurrentUser`. 2. Then run `Invoke-PS2EXE -InputFile "C:Scriptsscript.ps1" -OutputFile "C:Scriptsscript.exe" -IconFile "C:IconsMailIcon.ico" -NoConsole -Verbose`. Just a heads up, though, this requires creating a custom .exe, which isnโt what youโre looking for but might be a solid workaround!
42plus17: Yes, I wanted to avoid creating a custom exe. I get that many of these tools are C# applications compiled into exe. I'll give your suggestion a shot, or I might just learn enough C# to create my own runner if nothing else works. Thanks!
Reply to this answer
Add your answer
Answers are reviewed before publication.
42plus17: Oh wow, it works. Thank you!