Sneaky PowerShell Trick: Run Completely Without A Window

Maybe you have a login script or something else that’s written in PowerShell that you want to run without having any kind of window pop up – not even a blank one. There’s a few ways to do this, but my current favorite is to wrap it in C#. Thanks Mark Kraus for this tip!

Fire up Visual Studio, and create a new C# console application. Right click and change the properties of the project so that the output type is Window App… and then get this… don’t make a window.

From there, you’ll need to add a reference for PowerShellStandard.Library so you can do PowerShell-y business in your C# application. After that, you can make a method look like this.

It’s not super complicated code. Line 3 creates a new PowerShell object so we can, you know, run PowerShell code. On Line 4 – 6, we’re adding the script that’s going to be executed. You could retrieve the code from a file, but I’ve just stuck a here-string in there. My script is pretty simple, it’s just getting all the items in my c:\temp folder and exporting that output to a file named shh.txt.

After that, I’ve got to actually run my code. That happens on Line 7, and then on Line 8 and 9, we make the thread wait until the code is done executing. Then finally on Line 10 and 11, I’m cleaning up after myself by ending the invocation and disposing of my PowerShell object.

You can build this, and then look in the folder for your app, in the bin\Debug folder, you’ll find an .exe file which you can run. Double click it and you shouldn’t see any window pop up, no console, no XAML window, no nothing, but your PowerShell script will run. If you made yours look like mine, go check for that file that it should have created.

Now when your login script runs, your users won’t be able to close it before it finishes!

3 thoughts on “Sneaky PowerShell Trick: Run Completely Without A Window

  1. In regards to adding this for code completeness. (I always feel dumb when I read the code, then can’t figure out the using statements or assembly references I need to get this to work.)
    1.) Add the NuGet package PowerShellStandard.Library ; this is a ‘prerelease’ package right now so in visual studio you have to check the “Include prerelease” box.
    2.) add the using statement
    using System.Management.Automation;

  2. Very nice. My current favorite is:

    Add-Type -Name Window -Namespace Console -MemberDefinition '
    public static extern IntPtr GetConsoleWindow();

    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    $consolePtr = [Console.Window]::GetConsoleWindow()
    [Console.Window]::ShowWindow($consolePtr, 0)

    [Console.Window]::ShowWindow($consolePtr, 5) # just in case we want the console window back

    This has the benefit of being done completely in powershell.

    1. Corbob,

      Hi. I was looking for a way to do that to use with a wallpaper GPO.
      The only way I found, was to use a VBS file with this code:

      Set WshShell = CreateObject(“WScript.Shell” )
      WshShell.Run chr(34) & “C:\Windows\Web\Wallpaper\Organization\Wallpaper-1024768.bat” & Chr(34), 0
      Set WshShell = Nothing

      The problem is that I need a VBS and a BAT file.
      Using your code, where can I put the code to be run without showing any window?

Leave a Reply

Your email address will not be published. Required fields are marked *