脚本只是保存在文本文件(使用特殊的.ps1扩展名)中的命令集合, PowerShell可以理解并按顺序执行以执行不同的操作。在这篇文章中,我们将概述如何在Windows 11/10PowerShell脚本文件所涉及的过程。
PowerShell是Microsoft设计的命令行工具,用于运行命令和脚本以更改设置和自动执行任务。在某种程度上,它类似于Command Prompt。但是,PowerShell是一个功能更强大的命令行界面 ( CLI ),它提供了广泛的工具集以及更大的灵活性和控制力。此外,与命令提示符不同,PowerShell可在Windows、macOS 和Linux 上(Linux)使用。
要了解如何在Windows 11/10PowerShell脚本文件,请按照相应部分中显示的每个任务的说明进行操作。
如何创建PowerShell脚本文件
在Windows 11/10上,您几乎可以使用任何文本编辑器或ISE(集成脚本环境(Integrated Scripting Environment))控制台创建PowerShell脚本文件。(PowerShell)但是,构建脚本的首选选项是使用带有PowerShell扩展的 Visual Studio Code编辑器。
Visual Studio Code(也称为VS Code)是一个免费且可扩展的跨平台代码编辑器,它提供了一个几乎可以编辑任何类型编程语言的环境。添加PowerShell扩展时,您可以获得完全交互式的脚本编辑体验,即使支持IntelliSense(代码完成)。
以下是使用VS Code在(VS Code)Windows 11/10上创建PowerShell脚本文件的方法:
- 前往 Visual Studio 下载页面(Head to Visual Studio Download page)。
- 单击 Windows 按钮下载安装程序。
- 双击(Double-click)下载的文件开始VS Code的安装过程。
- 确认协议条款。
- 单击 下一步(Next) 按钮。
- 再次单击 下一步(Next) 按钮。
- 再次单击 下一步(Next) 按钮。
- 根据需要确认(Confirm)其他任务。
- 单击 下一步(Next) 按钮。
- 单击 安装(Install) 按钮。
- 单击 完成(Finish) 按钮。
完成这些步骤后,您可以继续安装PowerShell扩展。就是这样:
- 打开 VS 代码(VS Code)。
- 单击左窗格中的 扩展(Extensions)选项卡或按CTRL + SHIFT + X组合键。
- 搜索 PowerShell 并选择最上面的结果。
- 单击 安装(Install) 按钮。
完成安装步骤后,您可以开始使用 Visual Studio Code编写(Studio Code)PowerShell脚本。就是这样:
- 打开 VS 代码(VS Code)。
- 单击 文件(File) 菜单并选择 新建文件(New File) 选项。
- 单击 文件(File) 菜单并选择 另存为(Save as) 选项。
- 在文件名字段中,为具有(File name).ps1扩展名的文件指定一个名称, 例如 TWC_script.ps1。
- 单击 保存(Save) 按钮。
编写一个新的或粘贴您要运行的脚本——例如:
Write-Host "Welcome to TheWindowsClub.com! Your first script executed successfully"
上面的脚本将在屏幕上输出下面的短语。
Welcome to TheWindowsClub.com! Your first script executed successfully
您可以单击右上角的运行(Run) 按钮(或按 F5 键)运行脚本。
- 单击 文件(File) 菜单。
- 单击 保存(Save) 选项。
使用 Visual Studio Code完成这些步骤后,脚本就可以运行了,但默认情况下会失败。这是因为默认的 PowerShell 设置始终设置为阻止任何脚本的执行。(唯一的例外是在 Visual Studio Code或PowerShell ISE中运行脚本的内容。)
如何在 Windows 11/10 上运行PowerShell脚本文件(PowerShell)
要使用PowerShell(PowerShell)运行脚本文件,您必须更改Windows 11/10上的执行策略。
在Windows 11/10上,PowerShell包括四个执行策略,包括:
- Restricted — 停止运行任何脚本。
- RemoteSigned — 允许(Allows)在设备上创建脚本,但在另一台计算机上创建的脚本不会运行,除非它们包含受信任的发布者的签名。
- AllSigned — 所有脚本都将运行,但前提是受信任的发布者已签署它们。
- Unrestricted — 运行(Runs)任何脚本,没有任何限制。
以下是在 Windows 11/10 上运行PowerShell脚本文件的方法:(PowerShell)
- 按Windows key + X打开高级用户菜单(open Power User Menu)。
- 按键盘上的A以管理员/提升模式启动(A)PowerShell。
- 在PowerShell控制台中,键入以下命令并按Enter 键(Enter)。
Set-ExecutionPolicy RemoteSigned
接下来,键入下面的命令并按Enter 键(Enter)运行脚本。确保(Make)将“PATHTOSCRIPT”占位符更改为脚本的位置。
& "C:\PATH\TO\SCRIPT\TWC_script.ps1"
完成这些步骤后,脚本将运行,如果编写正确,您应该可以看到其输出没有问题。
这就是如何在Windows 11/10PowerShell脚本文件!
相关(Related):如何在不签名的情况下运行本地 PowerShell 脚本(How to run local PowerShell Scripts without Signing)。
How to create and run PowerShell script file on Windows 11/10
A scrіpt iѕ just a collection of commands saved intо a text file (using the special .ps1 extension) that PowerShell understands and executes in sequence to perform different actions. In this post, we will outline the process involved in how to create and run a PowerShell script file on Windows 11/10.
PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.
To see how to create and run the PowerShell script file on Windows 11/10, follow the instructions for each of the tasks as shown in the respective sections.
How to create a PowerShell script file
On Windows 11/10, you can create PowerShell script files using virtually any text editor or the ISE (Integrated Scripting Environment) console. However, the preferred option to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.
Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.
Here’s how to create PowerShell script file on Windows 11/10 using VS Code:
- Head to Visual Studio Download page.
- Click the Windows button to download the installer.
- Double-click the downloaded file to start the installation process of VS Code.
- Confirm the agreement terms.
- Click the Next button.
- Click the Next button again.
- Click the Next button one more time.
- Confirm additional tasks as necessary.
- Click the Next button.
- Click the Install button.
- Click the Finish button.
Once you complete the steps, you can proceed to install the PowerShell extension. Here’s how:
- Open VS Code.
- Click the Extensions tab from the left pane or press the CTRL + SHIFT + X key combo.
- Search for PowerShell and select the top result.
- Click the Install button.
After you complete the installation steps, you can start writing PowerShell scripts using Visual Studio Code. Here’s how:
- Open VS Code.
- Click the File menu and select the New File option.
- Click the File menu and select the Save as an option.
- In the File name field specify a name for the file with the .ps1 extension — for example, TWC_script.ps1.
- Click the Save button.
Write a new, or paste the script you want to run — for example:
Write-Host "Welcome to TheWindowsClub.com! Your first script executed successfully"
The above script will output the phrase below on the screen.
Welcome to TheWindowsClub.com! Your first script executed successfully
You can click the Run button from the top-right side (or press the F5 key) to run the script.
- Click the File menu.
- Click the Save option.
Once you complete the steps using Visual Studio Code, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)
How to run PowerShell script file on Windows 11/10
To run a script file with PowerShell, you have to change the execution policy on Windows 11/10.
On Windows 11/10, PowerShell includes four execution policies, including:
- Restricted — Stops any script from running.
- RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
- AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
- Unrestricted — Runs any script without any restrictions.
Here’s how to run PowerShell script file on Windows 11/10:
- Press Windows key + X to open Power User Menu.
- Press A on the keyboard to launch PowerShell in admin/elevated mode.
- In the PowerShell console, type in the command below and hit Enter.
Set-ExecutionPolicy RemoteSigned
- Type A and hit Enter (if applicable).
Next, type the command below and hit Enter to run the script. Make sure to change “PATH\TO\SCRIPT” placeholder to the location of your script.
& "C:\PATH\TO\SCRIPT\TWC_script.ps1"
After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.
That’s it on how to create and run the PowerShell script file on Windows 11/10!
Related: How to run local PowerShell Scripts without Signing.