In the Windows operating system, PowerShell scripts are powerful tools for automating tasks such as file management, system configuration, and program calls, which can greatly improve work efficiency. However, many beginners who are new to PowerShell often encounter three annoying problems when writing and running scripts: double-clicking the script opens Notepad instead of running it, the PowerShell window flashes and closes immediately when executing the script, and Chinese characters output by the script are displayed as garbled text. These problems may seem complicated, but they all have straightforward solutions. In this article, we will detailedly explain the causes of each problem and provide step-by-step solutions to help you get rid of these troubles and run PowerShell scripts smoothly.
1. Problem 1: Why Does Double-Clicking A PowerShell Script Open Notepad Instead Of Running It?
After creating a PowerShell script file with the `.ps1` extension, many users find that double-clicking the file does not open the PowerShell window to run the script, but instead opens it in Notepad, displaying the script source code. This happens because Windows does not natively associate `.ps1` files with the PowerShell program, so it uses the default text editor (Notepad) to open them. To solve this problem, you only need to manually set the default program for opening `.ps1` files. Here are the detailed steps:
- Locate the PowerShell script file (with the `.ps1` extension) you created, and right-click on the file with your mouse;
- In the pop-up right-click menu, select the “Open with” option;
- In the “Open with” settings interface, click “Choose another app”;
- In the newly popped-up window, click “More apps” at the bottom, and then select “Look for another app on this PC”;
- In the file explorer window that appears, navigate to the path `C:\Windows\System32\WindowsPowerShell\v1.0`;
- In this path, find the `powershell.exe` file, select it, and click the “Open” button;
- Return to the “Open with” window, check the box next to “Always use this app to open .ps1 files”, and then click “OK”.
After completing these steps, double-clicking the PowerShell script file will automatically open the PowerShell window to run the script instead of opening it in Notepad.
2. Problem 2: Why Does The PowerShell Window Flash And Close Immediately When Running A Script?
After solving the file association problem, some users may find that although double-clicking the script opens the PowerShell window, the window flashes and closes immediately, making it impossible to see the running results. What causes this? Actually, this is because the PowerShell execution policy of the system restricts script execution, causing the script to exit quickly without running normally. Let’s solve this problem in detail with the following steps:
- Click the search box at the bottom of the Windows desktop and type “PowerShell”;
- In the search results, find “Windows PowerShell”, right-click on it with your mouse;
- In the right-click menu, select “Run as administrator” to open a PowerShell window with administrative privileges;
- First, we need to check the current user’s PowerShell execution policy. In the window, type the command `Get-ExecutionPolicy -List` and press the Enter key;
- After running the command, the execution policies for each scope in the current system will be displayed. Usually, by default, the execution policy for the current user is “Undefined”, which means the system prohibits running scripts;
- To allow script execution, we need to modify the execution policy. Type the command `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser` and press the Enter key;
- At this time, the system will pop up a prompt asking if you want to change the execution policy. Type “Y” (which means yes) and press the Enter key;
- After the execution policy is modified successfully, we can verify it. Type the command `Get-ExecutionPolicy` and press the Enter key. If it displays “RemoteSigned”, it means the modification is successful;
- Next, navigate to the directory where your PowerShell script file is located (you can use the `cd script file path` command);
- In the window, type the name of the script file (including the `.ps1` extension) and press the Enter key. At this time, the script can run normally, and the window will not flash and close.
- It should be noted here that the “RemoteSigned” execution policy allows running locally written scripts, while scripts downloaded from the Internet must have a digital signature to run. This ensures the executability of the script and guarantees system security to a certain extent.
3. Problem 3: How To Fix Chinese Garbled Characters When Outputting From PowerShell Scripts?
When we add Chinese content to a PowerShell script (such as outputting prompt text like “Pause” or “Run successfully”) and run it, we often find that the Chinese characters are displayed as garbled text. This is because the encoding format of the script does not match the output encoding format of the PowerShell terminal. Below, we will teach you how to fix the Chinese garbled character problem in two steps. Here are the detailed steps:
- First, open your PowerShell script file with Notepad: right-click on the script file and select “Edit in Notepad”;
- At the beginning of the script file, add two lines of code to set the terminal output encoding. The specific code is as follows:
$OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
- After adding the code, click “File” in the Notepad menu bar and select “Save As”;
- In the “Save As” window, set “Save as type” to “All Files” to ensure the file name and extension remain unchanged;
- The key step: in the “Encoding” drop-down menu at the bottom of the window, select “UTF-8”, then click “Save” to overwrite the original script file;
- After saving successfully, close Notepad and return to File Explorer;
- Double-click the modified PowerShell script file to run it. At this time, the Chinese characters output in the script will be displayed normally, and there will be no more garbled text.
4. Conclusion: Master These Tips To Easily Use PowerShell Scripts
The above are the detailed solutions to the three most common problems encountered when writing and running PowerShell scripts. From setting the default file opening method, to modifying the execution policy to solve the problem of the window flashing and closing, and then adjusting the encoding format to resolve Chinese garbled characters, each step is clear and easy to understand, even for beginners with zero foundation can follow the operations successfully. PowerShell scripts are very powerful tools in the Windows system. Mastering the solutions to these basic problems can make you more smooth when using scripts to implement automated operations and improve work and learning efficiency.
5. Demo Video
You can watch the following demo video by select the subtitle to your preferred subtitle language.