To run a PowerShell script, first adjust the execution policy if needed by running Set-ExecutionPolicy RemoteSigned as Administrator when Get-ExecutionPolicy returns Restricted; then save your script with a .ps1 extension like C:\Scripts\hello.ps1; navigate to its directory using cd C:\Scripts or use the full path to execute it via .\hello.ps1 or PowerShell.exe -File "C:\Scripts\hello.ps1"; alternatively, right-click the file and select "Run with PowerShell" if available; resolve common issues by unblocking downloaded files with Unblock-File -Path C:\Scripts\hello.ps1 and ensuring correct paths are used; for better editing and debugging, consider using VS Code with the PowerShell extension or PowerShell ISE.
Running a PowerShell script in Windows is straightforward, but there are a few important steps and settings you need to be aware of—especially around execution policies. Here’s how to do it properly.

1. Create or Locate Your PowerShell Script
A PowerShell script is a file with a .ps1
extension. For example, myscript.ps1
. You can create one using Notepad or any code editor:
# Example: hello.ps1 Write-Output "Hello, World!"
Save it with a .ps1
extension, preferably in a simple path like C:\Scripts\hello.ps1
.

2. Check the Execution Policy
By default, Windows may block script execution for security. Open PowerShell as Administrator and run:
Get-ExecutionPolicy
If it returns Restricted
, scripts won’t run. To allow local scripts, use:

Set-ExecutionPolicy RemoteSigned
RemoteSigned
means only scripts from the internet need signing; local scripts can run.- Confirm the change when prompted.
?? Be cautious with execution policies in enterprise environments—check with your IT policy first.
3. Run the Script
You can run the script in several ways:
Option A: Using PowerShell
Open PowerShell (no need for admin unless the script requires it) and run:
.\hello.ps1
Make sure you’re in the directory where the script is located. Use cd C:\Scripts
to navigate.
Option B: Run from Any Location
Use the full path:
C:\Scripts\hello.ps1
Or with the PowerShell.exe -File
command:
PowerShell.exe -File "C:\Scripts\hello.ps1"
This is useful for shortcuts or Task Scheduler.
Option C: Right-Click and Run
You can also:
- Right-click the
.ps1
file - Choose "Run with PowerShell"
Note: This option may not appear if you’ve changed default app associations.
4. Fix Common Issues
"Cannot be loaded because running scripts is disabled"
→ Go back to Step 2 and adjust the execution policy."Permission denied" or script doesn’t launch
→ Make sure you’re allowed to execute scripts and the file isn’t blocked.Unblock a downloaded script with:
Unblock-File -Path C:\Scripts\hello.ps1
Path issues
→ Always use the full path or navigate to the script’s directory first.- Use PowerShell ISE (built-in on older Windows)
- Or VS Code with the PowerShell extension (recommended)
5. Optional: Use PowerShell ISE or VS Code
For editing and running scripts more easily:
These give better debugging, syntax highlighting, and one-click run options.
Basically, just make sure execution is allowed, navigate to your script, and run it with .\scriptname.ps1
. The key hurdle is usually the execution policy—not the command itself.
The above is the detailed content of How to run a PowerShell script in Windows. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The problem of slow booting can be solved by the following methods: 1. Check and disable unnecessary booting programs; 2. Turn off the quick boot function; 3. Update the driver and check disk health; 4. Adjust the number of processor cores (only for advanced users). For Windows 11 systems, first, the default self-start software such as QQ and WeChat are disabled through the task manager to improve the startup speed; if you use dual systems or old hardware, you can enter the power option to turn off the quick boot function; second, use the device manager to update the driver and run the chkdsk command to fix disk errors, and it is recommended to replace the mechanical hard disk with SSD; for multi-core CPU users, the kernel parameters can be adjusted through bcdedit and msconfig to optimize the startup efficiency. Most cases can be corrected by basic investigation

If you're having trouble reading your desktop icons' text or simply want to personalize your desktop look, you may be looking for a way to change the font color on desktop icons in Windows 11. Unfortunately, Windows 11 doesn't offer an easy built-in

Fixed Windows 11 Google Chrome not opening Google Chrome is the most popular browser right now, but even it sometimes requires help to open on Windows. Then follow the on-screen instructions to complete the process. After completing the above steps, launch Google Chrome again to see if it works properly now. 5. Delete Chrome User Profile If you are still having problems, it may be time to delete Chrome User Profile. This will delete all your personal information, so be sure to back up all relevant data. Typically, you delete the Chrome user profile through the browser itself. But given that you can't open it, here's another way: Turn on Windo

When Windows cannot detect a second monitor, first check whether the physical connection is normal, including power supply, cable plug-in and interface compatibility, and try to replace the cable or adapter; secondly, update or reinstall the graphics card driver through the Device Manager, and roll back the driver version if necessary; then manually click "Detection" in the display settings to identify the monitor to confirm whether it is correctly identified by the system; finally check whether the monitor input source is switched to the corresponding interface, and confirm whether the graphics card output port connected to the cable is correct. Following the above steps to check in turn, most dual-screen recognition problems can usually be solved.

Have problems uploading files in Google Chrome? This may be annoying, right? Whether you are attaching documents to emails, sharing images on social media, or submitting important files for work or school, a smooth file upload process is crucial. So, it can be frustrating if your file uploads continue to fail in Chrome on Windows PC. If you're not ready to give up your favorite browser, here are some tips for fixes that can't upload files on Windows Google Chrome 1. Start with Universal Repair Before we learn about any advanced troubleshooting tips, it's best to try some of the basic solutions mentioned below. Troubleshooting Internet connection issues: Internet connection

Mini PCs have undergone

Microsoft's latest Windows updates can generally be installed safely, but they need to be judged based on the update type and usage scenario. Ordinary users can update their daily office work, video watching, etc. directly; professional software or game users should be cautious. Regular quality updates (such as the monthly "Tuesday Patch") have low risks, so it is recommended to install them in time; updates to functions (such as large version upgrades) may cause compatibility issues. It is recommended to back up data, confirm software and hardware support, and check community feedback before installing. Overall, quality updates are safe and reliable, and functional updates are suitable for optional installation after observation.
