sodcalculator

10 Tech How-To

Run a PowerShell Script During Windows Installation?

Guide 10-41821 · Updated · Step by step

Running a PowerShell script during Windows installation can change settings, install or remove software, create accounts, and alter files—but it does not automatically change everything on the computer. What happens depends on when the script runs, the commands it contains, and the permissions available at that stage of setup.

What “during Windows installation” can mean

The phrase can refer to several different points in setting up Windows, and they do not all provide the same environment or permissions. A script run before Windows is installed may operate in Windows Preinstallation Environment (Windows PE), a limited environment used for setup and deployment. A script run during the first-start experience or after the desktop appears runs in a more complete Windows environment.

Windows Setup supports automation and customization for managed deployments. Administrators can configure setup to run commands at particular stages, while an individual user might instead open a command prompt or run a script after installation. The exact behavior depends on the setup method and configuration; simply having a .ps1 file on a USB drive does not make Windows run it.

  • Before or during setup: Commands may work on the installation environment or on the Windows image, depending on how they are launched.
  • At first start: A configured setup command can make changes as Windows is being prepared for its first user.
  • After reaching the desktop: The script runs within the installed system, subject to the account’s permissions and PowerShell’s execution settings.

What a script can change

PowerShell is a command shell and scripting language. A script can combine commands, so its effects may be much broader than its filename or a brief description suggests. A script running with administrator privileges can make system-wide changes; a standard-user script is generally limited to actions that user is allowed to perform.

Depending on its commands and context, a script may:

  • Change registry values, Windows settings, services, scheduled tasks, or firewall rules.
  • Install, configure, or remove applications and Windows features.
  • Create or modify local accounts, groups, folders, and files.
  • Copy files from installation media or a network location, or run other programs.
  • Change settings that affect all users, if it has sufficient privileges.

It can also do very little. For example, a script that only displays a message or checks a setting will not wipe a drive. The impact is determined by the commands that actually execute, including commands called by other scripts or programs.

How scripts are launched and permissions work

PowerShell scripts usually have a .ps1 extension. Windows Setup does not normally execute every script it finds. A setup answer file, deployment tool, administrator, or user must arrange for it to run. During a managed deployment, commands can be attached to setup stages; in an ordinary installation, a person commonly runs a script later from PowerShell or another launcher.

Two separate controls matter: when and how the script is launched, and what permissions its process has. Running PowerShell as administrator can permit elevated changes, but it does not make every operation succeed. Some operations still depend on available components, network access, policy, or the installation environment. A process running in Windows PE, for instance, should not be assumed to have the same tools and services as a fully installed Windows system.

PowerShell’s execution policy is another factor. It can restrict which scripts run in a particular configuration, but it is not a complete security boundary and should not be treated as proof that a script is safe. A policy setting that allows a script to launch says nothing about whether its commands are trustworthy.

How to inspect a script before running it

If you received a script with installation instructions, inspect it before starting setup or executing it. Open the file as text in a suitable editor, rather than double-clicking it, and review the commands and any files it invokes. Look for destructive or high-impact actions, such as deleting files, changing partitions, modifying security settings, creating accounts, downloading programs, or launching other scripts.

  1. Confirm the source. A familiar filename or website description is not enough; make sure the file came from the source you intended to use.
  2. Read the whole script. Check for encoded or hard-to-read commands, downloaded content, and references to additional files.
  3. Check its run stage and account. Find out whether it runs before, during, or after setup and whether it runs with administrator privileges.
  4. Protect data and recovery options. Back up important files before installation, and make sure you have a way to reinstall or recover Windows if the changes prevent startup.
  5. Use a test environment when appropriate. Organizations can test deployment scripts on a virtual machine or non-production device before using them broadly.

Do not run a script merely because a guide says to paste a command into PowerShell. If you cannot understand what it will do, ask the provider or a knowledgeable administrator to explain it first.

Common problems and what to expect

A script may fail because it is launched at the wrong setup stage, lacks administrator rights, expects a command or feature that is unavailable, or refers to a file or network location that cannot be reached. A script can also be blocked by execution settings or organization policy. An error message is useful evidence, but changing policy or elevating privileges without understanding the cause can create new risks.

Some setup changes are hard to reverse. Removing an application may delete its configuration; changing registry values or services may affect startup; repartitioning or formatting a drive can erase data. A restore point may not exist during early setup and should not be relied on as a substitute for a separate backup. If setup becomes unstable after a script runs, record the error and identify the changes before rerunning it. Repeating the script may apply changes twice or make recovery harder.

PowerShell scripts compared with other setup methods

A PowerShell script is flexible and can automate multiple tasks, but that flexibility makes careful review important. Windows setup answer files can configure selected installation choices, while management and deployment tools can apply settings in a controlled, repeatable process. Manual configuration is slower but may be easier to understand for a small number of changes.

These approaches are not interchangeable in every situation. Some changes require a fully running Windows installation; others are best handled by deployment tooling or documented Windows setup options. If your goal is only to install Windows, you do not need to run an unrelated script. If an organization provides a script, follow its documented procedure and ask its administrator which changes it makes.

Frequently Asked Questions

Will a PowerShell script erase my files during Windows installation?
Not automatically. It can erase or overwrite files if it contains commands that do so and has access to the target. Review the script and back up important data before running it.

Does Windows run PowerShell scripts automatically during setup?
Finding a .ps1 file on installation media does not normally cause it to run. A person, setup configuration, or deployment process must launch it.

Can a script change settings for every user?
Yes, if it has the necessary permissions and uses commands that make system-wide changes. A script running without elevation may be limited to the current user or other permitted actions.

Is changing PowerShell’s execution policy enough to make a script safe?
No. Execution policy affects whether scripts are allowed to run in a given configuration; it does not establish that their contents are safe or trustworthy.

What should I do if a script fails during setup?
Note the error and the setup stage, then check the script’s requirements, file paths, permissions, and available network or Windows components. Avoid repeatedly running it or weakening security settings until you understand the cause.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →