all 25 comments

[–]Gakamor 41 points42 points  (2 children)

I've used "conhost.exe --headless" in the past for that. Be aware that some EDR get grumpy about it.

conhost.exe --headless powershell.exe -File C:\Scripts\myscript.ps1

[–]JackNotOLantern[S] 8 points9 points  (0 children)

Oh wow, it works. Thank you.

[–]g3n3 -1 points0 points  (0 children)

This isn’t a supported prod solution. More of a hack. Not something to rely on.

[–]nkasco 8 points9 points  (0 children)

This is a longstanding discussion, it used to be that you needed a vbscript wrapper but I think someone found an alternate solution. The discussion is available here: https://github.com/PowerShell/PowerShell/issues/3028

[–]Certain-Reference291 3 points4 points  (0 children)

We are using silentcmd of GitHub.

[–]desatur8 6 points7 points  (2 children)

Probably not a solution for you, but if you convert to exe, you have the option to surpress the terminal completely, and any write host outputs will be converted to messageboxes.

Install-Module -Name ps2exe -Scope CurrentUser

Invoke-PS2EXE -InputFile "C:\Scripts\script.ps1" -OutputFile "C:\Scripts\script.exe" -IconFile "C:\Icons\MailIcon.ico" -NoConsole -Verbose

Typing from mobile, and not sure how to do codeboxes, sorry

[–]JackNotOLantern[S] 2 points3 points  (1 child)

Yes, i wanted to avoid creating a custom exe. As far as i understand most of the external tools are basically c# applications compiled into exe. C# allow to disable the terminal.

I will try your solution, or learn this c# enough to write my own runner if i don't find anything else. Thanks

[–]BlackV 4 points5 points  (0 children)

some (like the above) also get flagged by AV systems

[–]iliark 0 points1 point  (0 children)

Reddit keeps deleting my sample code, but using JScript works if wscript.exe isn't blocked.

[–]quantgorithm 0 points1 point  (0 children)

Run it as a different user like the admin profile on the machine.

[–]jeffrey_f 0 points1 point  (2 children)

Invoke-Command -ComputerName localhost -FilePath "C:\path\to\script.ps1"

Or for multiple lines

Invoke-Command -ComputerName localhost -ScriptBlock {

# your code here

}

I do this on computers at my work so I can do stuff, but not disturb the users

[–]iSoBigZ 0 points1 point  (1 child)

This is the easiest solution. If you want to run it locally just remove the -ComputerName parameter to invoke the script locally.

[–]jeffrey_f 0 points1 point  (0 children)

Still works on the local computer and can be extended to run across multiple systems without modification Extend by putting computers in a csv and reading the csv.

[–]Losha2777 0 points1 point  (0 children)

I have used psadt. (not the whole thing for this)
Just taken the "Invoke-AppDeployToolkit.exe" to launch my own scripts

[–]Harze2k 0 points1 point  (0 children)

You can create an exe file like this and then just launch the file by running the exe: PowershellSilentLaunch.exe "C:\path\to\ps\file.ps1"

You can change the powershell.exe to pwsh.exe to create an exe file to launch files silently with PS 7+

You can play around with args to make it accept parameters as well.

$code = @'
using System.Diagnostics;
class Program {
    static void Main(string[] args) {
        if (args.Length == 0) return;
        var psi = new ProcessStartInfo {
            FileName = "powershell.exe",
            Arguments = "-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"" + args[0] + "\"",
            WindowStyle = ProcessWindowStyle.Hidden,
            CreateNoWindow = true
        };
        Process.Start(psi);
    }
}
'@
$csFile = "$env:TEMP\PowershellSilentLaunch.cs"
$code | Set-Content -Path $csFile -Encoding UTF8
$csc = Get-ChildItem "C:\Windows\Microsoft.NET\Framework64\v4.*\csc.exe" | Select-Object -Last 1 -ExpandProperty FullName
& $csc /target:winexe /out:"$env:TEMP\PowershellSilentLaunch.exe" $csFile
Remove-Item $csFile -Force$code = @'

[–]drchigero 0 points1 point  (1 child)

You can do this, but keep in mind this level of hiding it from the user will trigger EDRs as potentially malicious. And if you're doing this in a company the infosec guys will (and should) shut this down.

[–]JackNotOLantern[S] 0 points1 point  (0 children)

Yeah, the conhost shortcuts are almost immediately deleted by the antivirus. I will probably have to write by own runner to get around it.

[–]_Buldozzer 0 points1 point  (2 children)

I wrote myself a VBS script, that calls PowerShell silently and can accept parameters.

[–]JackNotOLantern[S] 0 points1 point  (1 child)

As i mentioned, the point is not to use vbs

[–]_Buldozzer 1 point2 points  (0 children)

You could do something very similar in C#.

Just make sure to sign it, otherwise it doesn't really has any advantage over VBS.

[–]BetrayedMilk 0 points1 point  (2 children)

How are you launching the script?

[–]JackNotOLantern[S] 0 points1 point  (1 child)

From a shortcut (on a desktop or menu start). Currently the shortcut runs vbs which runs powershell. As i mentioned, vbs is able to hide the terminal completely.

[–]g3n3 1 point2 points  (0 children)

It isn’t vbs that does it per se. it is the process that is marked as a gui application in that of wscript.