PowerShell是微软开发的用于自动化常见管理员任务的命令行工具。一个脚本是一组PowerShell命令,存储在一个文本文件中,并带有*.ps1文件扩展名。当执行PowerShell脚本时,解释器会读取文件并按顺序运行命令。
您可以使用任何文本编辑器(甚至是记事本)创建PowerShell脚本。但最好建议使用PowerShell脚本编辑器。Windows中包含的默认编辑器是PowerShell集成脚本环境(PowerShell ISE)。它包括有用的功能,如语法高亮显示、代码补全、语法检查、调试、自动保存、崩溃保护等。
微软有一个名为Visual Studio Code(简称 VS Code)的免费跨平台代码编辑器,可在 Windows、Linux 和 macOS 上使用。通过安装扩展,它支持多种编程语言,包括 PowerShell。
Visual Studio Code 的一个优点是它支持最新版本的 PowerShell,而 PowerShell ISE 只支持 PowerShell 版本 5.1。
如何运行 PowerShell 脚本(快速指南)
以下是运行 PowerShell 脚本的快速步骤。以下方法适用于没有必需参数且不向命令提示符返回输出的 PowerShell 脚本。
- 在文件资源管理器中右键单击 PowerShell 脚本。
- 从上下文菜单中选择使用 PowerShell 运行。
- 脚本将以 Bypass 执行策略运行。计算机或用户的执行策略不会更改。
有关编写和运行 PowerShell 脚本的更多详细信息,请继续阅读!
如何创建 PowerShell 脚本
有几种方法可以创建 PowerShell 脚本:
- 使用跨平台代码编辑器 Visual Studio Code
- 使用 PowerShell ISE
- 或者任何其他文本编辑器,例如NotePad
使用Visual Studio Code创建PowerShell脚本
Visual Studio Code在Windows系统中并不自带,因此需要从专门的Visual Studio网站下载。在我们的情况下,我们需要下载Windows版本,该版本适用于64位、32位和ARM系统。
安装完应用程序后,您需要按照以下不同步骤操作。首先,您需要从开始菜单中打开Visual Studio Code。

安装PowerShell扩展
通过单击下面显示的扩展图标或使用快捷键Ctrl + Shift + X来打开扩展菜单。

在搜索框中键入powershell,选择下图所示的最多下载选项,确保您下载的是由Microsoft提供的扩展程序。然后单击安装。

安装完成后,您将不会看到任何确认信息。但是,您会看到安装按钮已被替换为禁用和卸载选项。

如何使用Visual Studio Code创建PowerShell脚本
要创建新的PowerShell脚本,请转到文件 > 新建文件,或使用快捷键Ctrl + N。

要指定新文件为PowerShell脚本,请单击左下角的纯文本,或单击脚本窗格内的选择语言。任选项均会将您带至选择语言模式框。键入powershell,然后选择PowerShell。

这将触发PowerShell扩展,您将看到行为和功能发生变化。首先,您会注意到文件名旁边的图标将更改为PowerShell。另外,您会注意到PowerShell控制台将启动,您可以在那里执行PowerShell命令和脚本。

也可以通过使用*.ps1扩展名保存该文件来实现相同的结果。
在脚本窗格中键入以下命令,您将体验到诸如颜色编码、命令自动完成和语法提示等好处:
Write-Host "This is a Visual Studio Code script" Write-Host "Writing PowerShell Scripts is fun!"

注意:第三行仅供演示,展示命令完成选项。
要保存文件,可以使用文件菜单:文件 > 保存。您也可以使用快捷键Ctrl + S。
接下来,选择一个易于访问的位置,提供文件名,确保将*.ps1指定为文件扩展名,然后单击保存。在此示例中,我将文件存储在C:\TEMP\MyVSCodeScript.ps1中。

在本文稍后部分将介绍在VS Code中执行脚本。
使用Windows PowerShell ISE创建脚本
要启动PowerShell ISE,请单击“开始”按钮(或搜索按钮)并开始输入PowerShell ISE。您将在搜索结果中看到该应用程序,并提供不同的打开选项。始终建议使用以管理员身份运行,以确保所有命令都能正确执行且不会被阻止。

现在,让我们在PowerShell ISE中创建一个脚本。写下以下几行,您可以注意到一些方便的功能,如颜色编码、命令自动完成、语法提示等:
Write-Host "This is a PowerShell ISE script" Write-Host "Writing PowerShell Scripts is fun!"

注意:第三行仅用于演示目的,以展示命令完成选项。
现在,让我们将这些命令保存为PowerShell脚本。为此,您需要单击工具栏上的软盘图标,然后单击文件 > 保存。您还可以使用键盘快捷键Ctrl + S。
在另存为对话框中,选择一个文件夹,提供一个文件名,指定*.ps1扩展名,然后单击保存。在本例中,我提供以下名称:C:\TEMP\MyPowerShellISEScript.ps1。

使用记事本创建脚本
不建议使用基本文本编辑器来编写PowerShell脚本,但这是可能的。让我们以记事本为例:
-
打开记事本并输入以下命令:
Write-Host "This is a Notepad script" Write-Host "Writing PowerShell Scripts is fun!

-
要保存脚本,请选择文件 > 保存。
-
在另存为对话框中,选择一个文件夹并提供一个带有*.ps1扩展名的文件名。在本例中,我使用:C:\TEMP\MyNotepadScript.ps1。
如何运行PowerShell脚本
A PowerShell script (*.ps1 file) can be run in the PowerShell console, which recognizes the *.ps1 file type and runs the commands sequentially. You may open the script as a file in code editors like the PowerShell ISE and Visual Studio Code, and run the whole script in the console pane or run only a part of it.
如何通过更改执行策略来启用PowerShell脚本
PowerShell的执行策略是一种安全特性,它控制PowerShell在何种条件下加载配置文件和运行脚本。需要指出的是,这并不是一个安全系统,因为执行策略可以被轻易绕过。然而,它们有助于保护用户不意外执行脚本。
Windows 11的默认执行策略是受限的。在非Windows操作系统上,默认执行策略是无限制的,并且无法更改。
要查看当前的执行策略,请使用PowerShell命令Get-ExecutionPolicy。
您可以使用Set-ExecutionPolicy cmdlet在Windows上更改执行策略:
Set-ExecutionPolicy -ExecutionPolicy <PolicyName>
不同的执行策略将在下一个主题中讨论。
更改PowerShell执行策略
在Windows上,您可以为本地机器、当前用户或PowerShell进程设置执行策略。
要列出所有可用的作用域,请使用Get-ExecutionPolicy -List。
Get-ExecutionPolicy -List
顺序很重要,因此配置的最高项具有优先权。例如,如果您使用组策略设置了执行策略,它将优先于其他策略。

Policy | Description |
Restricted | The default execution policy for Windows client OSes. It does not allow ANY scripts (*.ps1 files) to be executed. Still, you may run individual commands. |
RemoteSigned | The default execution policy for Windows Server. It Allows running scripts that are created locally. Scripts downloaded from untrusted locations, like the Internet, e-mail, messengers, etc. must be digitally signed by a trusted publisher. You may use the command Unblock-File to allow a script to run on the system. |
Unrestricted | The default execution policy for non-Windows computers, and it cannot be changed. It allows unsigned scripts to run, but it shows a warning message and asks for confirmation if scripts are coming from an untrusted location. |
AllSigned | It requires all scripts running on the machine to be digitally signed by a trusted publisher, no matter if they are created locally on the machine or downloaded from the Internet. |
Bypass | It allows all scripts to run, like Unrestricted, but no confirmation is required. |
Undefined | There is no execution policy set on the specified scope. If all scopes are set as undefined, then the default execution policies are applied. |
要允许PowerShell运行脚本,您需要使用Set-ExecutionPolicy -ExecutionPolicy
Set-ExecutionPolicy -ExecutionPolicy <PolicyName>
默认情况下,此命令将您选择的策略应用于本地计算机作用域。如果您想指定不同的作用域,则必须使用参数-Scope并提供作用域的名称。
例如,以下命令将执行策略设置为当前用户的无限制:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
使用PowerShell运行脚本
要打开PowerShell控制台,请单击“开始”按钮(或搜索按钮),输入powershell,然后单击以管理员身份运行。

要在PowerShell控制台中运行脚本,您可以:
- 使用脚本的全路径,例如:C:\TEMP\MyNotepadScript.ps1
- 或者仅使用脚本名称,从文件所在的文件夹中:.\MyNotepadScript.ps1
在Visual Studio Code中运行PowerShell脚本
要在Visual Studio Code中运行脚本,您首先需要启动应用程序并打开之前创建的脚本文件(C:\TEMP\MyVSCodeScript.ps1)。
- 选择 文件 > 打开 文件,或使用键盘快捷键 Ctrl + O。

- 在VS Code中加载脚本后,您可以通过单击右上角的运行按钮,或按下键盘上的F5来执行它。
脚本将在控制台窗格中执行,您将在那里看到脚本的输出。

另一种选择是仅运行脚本的一部分(运行选择)。这在您创建脚本时通常很有用,您可能希望确认脚本的部分行为是否如预期。
- 要运行脚本的一部分,请突出显示您希望执行的部分。
- 单击控制台右上角的运行选择,或按下键盘上的F8。
这次,VS Code仅在控制台窗格中执行所选的代码行。

在PowerShell ISE中运行脚本
在PowerShell ISE中运行脚本非常类似。
- 启动PowerShell ISE。
- 通过文件 > 打开,或工具栏上的打开图标,或键盘快捷键Ctrl + O,打开之前演示的脚本(在这种情况下 – C:\TEMP\MyPowerShellISEScript.ps1)。
- 选择脚本文件并单击打开。
要执行整个脚本,请使用工具栏上的运行按钮,或按下键盘上的F5。这将执行脚本文件在控制台窗格中,返回结果。

如果您只需要执行脚本的一部分,请突出显示该部分并从工具栏中选择运行选择,或者在键盘上按F8。同样,PowerShell ISE将只执行选定的代码行。

从命令提示符运行PowerShell脚本
如果*.ps1文件由PowerShell解释,那么命令提示符(CMD)不能直接与PowerShell脚本一起工作。如果您想在CMD中运行PowerShell脚本,您需要使用-File参数调用PowerShell进程来执行它,如下所示:
PowerShell -File C:\TEMP\MyNotepadScript.ps1.
结论
PowerShell脚本是一个很好的方式来自动化重复性任务。遵循这个基本规则:“如果您需要做某事超过一次,就编写脚本”,您就不会出错!
相关文章:
Source:
https://petri.com/how-to-write-and-run-a-powershell-script-file-on-windows-11/