How to Run a PowerShell Script Without Displaying a Window

Migel Hewage Nimesha Feb 02, 2024
  1. Use PowerShell’s -WindowStyle Hidden Parameter to Run a PowerShell Script Without Displaying a Window
  2. Use Task Scheduler to Run a PowerShell Script Without Displaying a Window
  3. Use Third-party Tools to Run a PowerShell Script Without Displaying a Window
  4. Conclusion
How to Run a PowerShell Script Without Displaying a Window

PowerShell by Microsoft is a tool that comes in default with Windows operating systems, enabling Windows users with automation functions based on command-line interfaces and commands.

PowerShell is a scripting tool and an automation tool; hence sometimes requires the application to run in the background silently. Through this article, we will see how to run the PowerShell application silently in the background, executing commands in the background.

There are third-party tools that you can incorporate with PowerShell tools to achieve the hidden functionality of PowerShell. Although, there are easy ways to do this with PowerShell only.

Use PowerShell’s -WindowStyle Hidden Parameter to Run a PowerShell Script Without Displaying a Window

One simple way to do the script window hiding would be by using the hidden type as a window style. A window-style PowerShell Window would be shown briefly at the beginning and then hidden during the script execution.

The -WindowStyle parameter in PowerShell allows you to control the appearance of the PowerShell window. The -WindowStyle Hidden parameter, when applied, ensures that the PowerShell window is not displayed during script execution.

This is especially useful for tasks that require automation without user interaction or when you don’t want to interrupt the user’s workflow.

Executing a PowerShell script with the -WindowStyle Hidden parameter is straightforward. Open a PowerShell window and use the following command:

PowerShell.exe -WindowStyle Hidden { Script you want to execute.. }

Here’s what each part of the command does:

  • PowerShell.exe: Initiates a new PowerShell process.
  • -WindowStyle Hidden: Sets the window style to hidden, preventing the PowerShell window from being displayed.
  • The script you want to execute is enclosed within curly braces {}.

PowerShell execution of the above command is shown in the below image.

PowerShell Execution

Once entered, the window disappears, yet the application will be running in the background. As shown in the below image of background processes, PowerShell runs as a background process.

Background process

In this method, the user would initially see the PowerShell. So it is not truly hidden from the end-users. Even if the task is scheduled previously, the window would pop up and get hidden quickly.

Use a Batch File

If you’re dealing with a more complex script or plan to run it multiple times, this method (batch file with PowerShell command) might be more organized and efficient.

Create a batch file (e.g., run_script.bat) with the following content:

@echo off
powershell -WindowStyle Hidden -File "C:\path\to\script.ps1"
  • @echo off: This command prevents the batch file from displaying its commands in the console window.
  • powershell -WindowStyle Hidden -File "C:\path\to\script.ps1": This line calls PowerShell with the necessary parameters and the path to the script.

Both the codes above achieve the same goal of running a PowerShell script without displaying a window, but they use slightly different approaches. However, there are other simple methods to achieve the true hidden functionality of PowerShell.

Use Task Scheduler to Run a PowerShell Script Without Displaying a Window

Task Scheduler is a Windows utility that enables you to automate tasks at specific times or events. It provides a wide range of options for customization, making it a versatile tool for managing various tasks on your computer.

Creating a New Task

  1. Open Task Scheduler: You can do this by searching for "Task Scheduler" in the Windows Start menu.
  2. Navigate to "Actions": Click on "Create Task" in the right-hand pane.

Configuring the Task

  1. Name the Task: Provide a name and description for your task. This should be something descriptive that reflects what the task does.
  2. Choose Task Trigger: Select how you want to trigger the task. Options include daily, weekly, monthly, at logon, etc. Choose the trigger that best fits your needs.

Setting up Triggers

  1. Select Trigger Details: Depending on the trigger you choose, you will need to provide additional details. For example, if you choose "Daily", you will specify the time and frequency.
  2. Advanced Settings: You can set more advanced options by clicking on "Advanced settings". This allows you to further customize when and how often the task runs.

Specifying Actions

  1. Select Action: In the "Actions" step, choose "New" > "Start a program" since we’ll be running a PowerShell script.
  2. Program/script: Browse and select powershell.exe on your system.
  3. Add arguments (optional): Enter the following:
-WindowStyle Hidden -File "C:\path\to\script.ps1"

Replace "C:\path\to\your\script.ps1" with the actual path to your PowerShell script.

Start a program

Enabling Security Options

Check the box that says "Run with highest privileges". This ensures that the script will run with administrator privileges if required.

You can also click Run whether user is logged on or not, and it will never show the PowerShell window when the task runs.

Task Scheduler

Completing and Testing the Task

Review the summary of the task and click "OK" to create it.

You can test the task by right-clicking on it in Task Scheduler and selecting "Run". This will execute the task immediately according to the triggers you’ve set.

Task Scheduler allows you to automate tasks at specified intervals or events. By using it to trigger PowerShell with the appropriate arguments, you can run scripts silently.

However, this method would affect the functionality of some scripts you try to use. Hence, you can also use third-party tools or community extensions for PowerShell.

Use Third-party Tools to Run a PowerShell Script Without Displaying a Window

Third-party tools extend the capabilities of PowerShell by providing additional functionalities and features. When it comes to running PowerShell scripts without displaying a window, these tools play a crucial role in automating tasks seamlessly.

Use PSExec

PSExec is a versatile tool from Sysinternals (now part of Microsoft) that allows you to execute processes remotely. It can be used to run PowerShell scripts silently.

psexec -d -s powershell -WindowStyle Hidden -File "C:\path\to\script.ps1"
  • psexec: Initiates PSExec.
  • -d: Instructs PSExec not to wait for the command to complete.
  • -s: Runs the command with system permissions.
  • powershell -WindowStyle Hidden -File "C:\path\to\script.ps1": Executes the PowerShell script silently.

Use NSSM (Non-Sucking Service Manager)

NSSM is a service manager for Windows that allows you to run any application as a service. This can be particularly useful for running scripts silently in the background.

  1. Download and install NSSM from the official website.
  2. Open a command prompt with administrative privileges.
  3. Run the following command to create a new service:
nssm install <ServiceName> "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\path\to\script.ps1"
  • nssm install <ServiceName>: Installs a new service with the specified name.
  • "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe": Specifies the path to the PowerShell executable.
  • "-NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\path\to\script.ps1": Passes parameters to PowerShell, instructing it to run the script silently.

Conclusion

PowerShell is a powerful automation tool for Windows users, providing extensive command-line capabilities. In scenarios where silent background execution is necessary, several methods are available.

  • Using the -WindowStyle Hidden Parameter: This allows for brief visibility of the PowerShell window before hiding it during script execution. It’s suitable for tasks that require automation without user interaction.
  • Batch Files: Organized for running complex or repeated scripts. Batch files call PowerShell with the necessary parameters.
  • Task Scheduler: Offers automation at specific times or events. By configuring it to run PowerShell with the right parameters, scripts can be executed silently.
  • Third-party Tools (e.g., PSExec, NSSM): Extend PowerShell’s capabilities, enabling windowless script execution.

Choose the method that best suits your specific needs and workflow.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.