How to Create a PowerShell Script

Uncategorized · · 4 min read

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

Shell

Windows PowerShell 5.1 or PowerShell 7+

Runs on

Windows 7, 8, 10, 11, Windows Server 2012–2025

Best for

Silent software installs, system configuration, file operations, and scheduled automation.

Requires admin

Only 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:

Copy

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

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

Copy

powershell.exe -ExecutionPolicy Bypass -File ".\run-task.ps1"

RemoteSigned

Allows local scripts. Blocks downloaded scripts unless signed. Recommended for most environments.

Bypass

No restrictions. Standard for deployment tools such as Intune, SCCM, and PDQ.

Restricted

Windows 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 desktop

Right-click the .ps1 file → Run with PowerShell. Accept the UAC prompt.

From an open session

Open PowerShell via Start → right-click Windows PowerShellRun as administrator, then run the script.

From a deployment tool

Intune, 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:

Copy

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.

Copy

Set-Location -Path $PSScriptRoot$Installer = "setup.exe"$Arguments = @("/quiet", "/norestart")Start-Process -FilePath $Installer -ArgumentList $Arguments -Wait -NoNewWindowexit $LASTEXITCODE

$PSScriptRoot

Sets the working folder to the script’s own location. Prevents path errors when run from a different directory.

-Wait

Holds the script until the process finishes. Required to capture the exit code reliably.

-NoNewWindow

Runs the process in the current console. Use -WindowStyle Hidden to suppress any UI.

$LASTEXITCODE

Passes the installer’s exit code back to the calling process or deployment platform.

Common PowerShell commands for deployment scripts

Run an MSI silently

Start-Process msiexec.exe -ArgumentList "/i installer.msi /quiet /norestart" -Wait

Run an EXE silently

Start-Process -FilePath "setup.exe" -ArgumentList "/quiet","/norestart" -Wait -NoNewWindow

Check if a file exists

Test-Path "C:\Program Files\App\app.exe"

Read the exit code

$proc = Start-Process setup.exe -Wait -PassThru; $proc.ExitCode

Write output to log

Start-Transcript -Path "C:\Logs\install.log" -Append

Create a folder

New-Item -ItemType Directory -Path "C:\Temp\Install" -Force

Before you run: checklist

File extension

The file is saved as .ps1, not .txt or .ps1.txt.

Plain text editor

The file was saved from Notepad or VS Code, not Word or WordPad.

Execution policy

The local machine policy allows scripts, or you are using -ExecutionPolicy Bypass.

Working folder

The installer file is in the same folder as the script, or the full path is specified.

Elevated session

PowerShell was opened as Administrator if the script installs software or changes system settings.

Tested first

The 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.

Rating
be the first to rate

Comments

Loading comments…

    Leave a comment

    ✎ Edit