Advertisement

Windows scripting reference

How to Create a PowerShell Script

A PowerShell script is a .ps1 file that runs one or more commands automatically on Windows.

This guide covers how to create, save, and run a PowerShell script — including how to handle execution policy and run scripts with administrator privileges.

What is a PowerShell script?

A PowerShell script is a plain text file saved with a .ps1 extension. It runs commands in order without manual input, making it useful for software deployment, system configuration, and repeatable admin tasks.

File extension.ps1
ShellWindows PowerShell 5.1 or PowerShell 7+
Runs onWindows 7, 8, 10, 11, Windows Server 2012–2025
Best forSilent software installs, system configuration, file operations, and scheduled automation.
Requires adminOnly when the commands inside need elevated privileges.

Step-by-step: create a PowerShell script

These steps work on any Windows version with PowerShell installed.

Create and save the script file

01
Create a working folder, for example C:\Scripts\MyTask.
02
Open Notepad or Visual Studio Code.
03
Paste your PowerShell commands into the editor.
04
Click FileSave As. Set Save as type to All Files.
05
Name the file with a .ps1 extension, for example run-task.ps1. Do not save as .txt.
06
Open PowerShell as Administrator and switch to the script folder.
07
Run the script with .\run-task.ps1 and verify the result.

Execution policy: fix the most common error

By default, Windows blocks unsigned PowerShell scripts. If you see the error “cannot be loaded because running scripts is disabled on this system”, change the execution policy before running the script.

Run this once in an elevated PowerShell session to allow local scripts:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

To bypass the policy for a single run without changing the system setting:

powershell.exe -ExecutionPolicy Bypass -File ".\run-task.ps1"
RemoteSignedAllows local scripts. Blocks downloaded scripts unless signed. Recommended for most environments.
BypassNo restrictions. Standard for deployment tools such as Intune, SCCM, and PDQ.
RestrictedWindows default. Blocks all script execution.

How to run a PowerShell script as administrator

Software installs and system changes require an elevated session. Use one of these methods.

From the desktopRight-click the .ps1 file → Run with PowerShell. Accept the UAC prompt.
From an open sessionOpen PowerShell via Start → right-click Windows PowerShellRun as administrator, then run the script.
From a deployment toolIntune, SCCM, and PDQ run scripts in the SYSTEM context by default — no UAC prompt needed.

To invoke a script as administrator from within another script or scheduled task:

Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `".\run-task.ps1`"" -Verb RunAs -Wait

PowerShell script template for silent deployment

A reliable starting structure for silent install and admin scripts on Windows.

Set-Location -Path $PSScriptRoot

$Installer = "setup.exe"
$Arguments = @("/quiet", "/norestart")

Start-Process -FilePath $Installer -ArgumentList $Arguments -Wait -NoNewWindow

exit $LASTEXITCODE
$PSScriptRootSets the working folder to the script’s own location. Prevents path errors when run from a different directory.
-WaitHolds the script until the process finishes. Required to capture the exit code reliably.
-NoNewWindowRuns the process in the current console. Use -WindowStyle Hidden to suppress any UI.
$LASTEXITCODEPasses the installer’s exit code back to the calling process or deployment platform.

Common PowerShell commands for deployment scripts

Run an MSI silentlyStart-Process msiexec.exe -ArgumentList "/i installer.msi /quiet /norestart" -Wait
Run an EXE silentlyStart-Process -FilePath "setup.exe" -ArgumentList "/quiet","/norestart" -Wait -NoNewWindow
Check if a file existsTest-Path "C:\Program Files\App\app.exe"
Read the exit code$proc = Start-Process setup.exe -Wait -PassThru; $proc.ExitCode
Write output to logStart-Transcript -Path "C:\Logs\install.log" -Append
Create a folderNew-Item -ItemType Directory -Path "C:\Temp\Install" -Force

Before you run: checklist

File extensionThe file is saved as .ps1, not .txt or .ps1.txt.
Plain text editorThe file was saved from Notepad or VS Code, not Word or WordPad.
Execution policyThe local machine policy allows scripts, or you are using -ExecutionPolicy Bypass.
Working folderThe installer file is in the same folder as the script, or the full path is specified.
Elevated sessionPowerShell was opened as Administrator if the script installs software or changes system settings.
Tested firstThe script was verified on one machine before deploying to production.

Frequently asked questions

What is the file extension for a PowerShell script?

PowerShell scripts use the .ps1 file extension. Windows does not run .ps1 files by double-clicking — you must run them from a PowerShell session or via a deployment tool.

Why does my PowerShell script say “running scripts is disabled on this system”?

This error means the execution policy is set to Restricted, which is the Windows default. Run Set-ExecutionPolicy RemoteSigned -Scope LocalMachine in an elevated PowerShell session to allow local scripts.

How do I run a PowerShell script as administrator?

Right-click the .ps1 file and select Run with PowerShell, then accept the UAC prompt. Alternatively, open PowerShell as administrator first, then run .\scriptname.ps1.

Can I run a PowerShell script silently with no window?

Yes. Use powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File ".\script.ps1". This is the standard approach for silent software deployment via Intune, SCCM, or PDQ.

How do I pass the exit code from a PowerShell script to Intune or SCCM?

End your script with exit $LASTEXITCODE. This passes the installer’s exit code back to the deployment platform, allowing it to detect success or failure correctly.

Advertisement