如何在Windows 11上編寫和運行PowerShell腳本文件

PowerShell是由微軟開發的命令行工具,用於自動化常見的管理任務。 腳本是一組PowerShell命令的集合,存儲在一個文本文件中,其擴展名為*.ps1。當執行PowerShell腳本時,解釋器讀取文件並按順序運行命令。

您可以使用任何文本編輯器(甚至是NotePad)來創建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腳本。

  1. 在檔案總管中右鍵點擊PowerShell腳本。
  2. 從上下文選單中選擇使用PowerShell運行
  3. 腳本將以Bypass執行原則運行。不會更改電腦或使用者的執行原則。

欲了解更多有關編寫和執行PowerShell腳本的詳細資訊,請繼續閱讀!

廣告

如何創建PowerShell腳本

有幾種方法可以創建PowerShell腳本:

  • 使用跨平台程式碼編輯器Visual Studio Code
  • 使用PowerShell ISE

使用 Visual Studio Code 創建 PowerShell 腳本

Visual Studio Code 在 Windows 上並不是預設安裝的應用程式,因此必須從專用的 Visual Studio 網站 下載。在我們的情況下,我們需要下載 Windows 版本,這可用於 64 位元、32 位元和 ARM 系統。

安裝應用程式後,請按照以下步驟進行操作。首先,您需要從開始選單中打開 Visual Studio Code。

Opening Visual Studio Code from the Windows 11 Start Menu

安裝 PowerShell 擴充功能

點擊下圖所示的擴充功能圖示,或使用鍵盤快捷鍵 Ctrl + Shift + X 來打開擴充功能選單。

廣告

Open the extensions menu from the left sidebar

在搜尋框中輸入 powershell,選擇下載量最大的選項,確保您下載的是 Microsoft 提供的擴充功能。然後點擊 安裝

Find the PowerShell extension

安裝完成後,您將不會看到任何確認訊息。但是,您會看到 安裝 按鈕已被取代為 停用解除安裝 選項。

Disable and Uninstall options appear after installing the extension

如何使用 Visual Studio Code 創建 PowerShell 腳本

要創建新的 PowerShell 腳本,請轉到 檔案 > 新檔案,或使用鍵盤快捷鍵 Ctrl + N。

Creating a new PowerShell script

要指定新文件為 PowerShell 腳本,請在左下角點擊純文字,或在腳本窗格中選擇選擇語言。任何一種選項都將帶您到選擇語言模式框中。輸入powershell,然後選擇PowerShell

Specify that the new file is a PowerShell script

這將觸發 PowerShell 擴展,您將看到行為和功能的變化。首先,您將注意到文件名旁邊的圖標將變為 PowerShell。另外,您將注意到 PowerShell 控制台將啟動,在這裡您將能夠執行 PowerShell 命令和腳本。

Starting the PowerShell Console

可以通過將文件保存為 *.ps1 擴展名來實現相同的結果。

在腳本窗格中輸入以下命令,您將體會到諸如語法高亮、自動命令完成和語法提示等功能帶來的好處:

Write-Host "This is a Visual Studio Code script"
Write-Host "Writing PowerShell Scripts is fun!"
How to type commands in the script pane

注意:第三行僅供演示目的,以展示命令完成選項。

要保存文件,您可以使用“文件”菜單:文件 > 儲存。您也可以使用快捷鍵 Ctrl + S。

接著,選擇一個易於訪問的位置,提供文件名,確保將 *.ps1 指定為文件擴展名,然後點擊儲存。在此示例中,我將文件存儲在 C:\TEMP\MyVSCodeScript.ps1 中。

Save your file as a .ps1

在本文後面將討論在 VS Code 中執行腳本。

使用 Windows PowerShell ISE 創建腳本

要啟動PowerShell ISE,請點擊開始按鈕(或搜索按鈕)並開始輸入PowerShell ISE。您將在搜索結果中看到該應用程序,並有不同的打開選項。始終建議使用以管理員身份運行,以確保所有命令都能正確執行,而不會被阻止。

You need to run the PowerShell ISE as an administrator

因此,讓我們在PowerShell ISE中創建一個腳本。輸入以下行,並再次,您可以注意到一些方便的功能,如顏色編碼、命令自動完成、語法提示等:

Write-Host "This is a PowerShell ISE script"
Write-Host "Writing PowerShell Scripts is fun!"

 

Creating a script in the PowerShell ISE

注意:第三行僅用於演示目的,以展示命令完成選項。

現在讓我們將這些命令保存為PowerShell腳本。為此,您需要點擊工具欄上的軟盤圖標,然後點擊文件 > 保存從文件菜單。您也可以使用鍵盤快捷鍵Ctrl + S。

另存為對話框中,選擇一個文件夾,提供一個文件名,指定*.ps1擴展名,然後點擊保存。在這個例子中,我提供以下名稱:C:\TEMP\MyPowerShellISEScript.ps1。

Saving your PowerShell script

使用NotePad創建腳本

不建議使用基本文本編輯器來編寫PowerShell腳本,但這是一種可能性。讓我們以NotePad為例:

  • 打開NotePad並輸入以下命令:

Write-Host "This is a Notepad script"
Write-Host "Writing PowerShell Scripts is fun!
You can write PowerShell scripts in NotePad
  • 要保存腳本,請選擇文件 > 保存

  • 另存為對話框中,選擇一個文件夾並提供一個帶有*.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命令在Windows上更改執行策略:

 Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

不同的執行策略將在下一個主題中討論。

更改PowerShell執行策略

在Windows上,您可以為本地機器、當前用戶或PowerShell進程設置執行策略。

若要列出所有可用的範圍,請使用 Get-ExecutionPolicy -List。

Get-ExecutionPolicy -List

順序很重要,因此配置的最高項目具有優先權。例如,如果您已使用群組原則設置執行原則,它將優先於其他原則。

The highest configured item takes precedence
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.
The different PowerShell execution policies

若要允許 PowerShell 運行腳本,您需要使用 Set-ExecutionPolicy -ExecutionPolicy <PolicyName> 命令。

Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

默認情況下,此命令將您選擇的策略應用於本地機器範圍。如果您想指定不同的範圍,則必須使用參數-Scope並提供範圍的名稱。

例如,以下命令將執行策略設置為當前用戶的無限制:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

在 PowerShell 中運行腳本

若要打開 PowerShell 控制台,請單擊“開始”按鈕(或搜索按鈕),輸入powershell,然後單擊以管理員身份運行

Run the PowerShell console as an administrator

若要在 PowerShell 控制台中運行腳本,您可以:

  • 使用腳本的完整路徑,例如:C:\TEMP\MyNotepadScript.ps1
  • 或者,僅使用腳本名稱,從文件所在文件夾:.\MyNotepadScript.ps1

在 Visual Studio Code 中運行 PowerShell 腳本

若要在 Visual Studio Code 中運行腳本,您首先需要啟動應用程序並打開之前創建的腳本文件(C:\TEMP\MyVSCodeScript.ps1)。

  • 選擇 檔案 > 開啟 檔案 或使用鍵盤快捷鍵 Ctrl + O.
  • 在 VS Code 中載入腳本後,您可以透過點擊右上角的 執行 按鈕,或按下鍵盤上的 F5 來執行它。

腳本將在控制台窗格中執行,您將在那裡看到腳本的輸出。

The script is being executed in the console pane

另一個選項是僅執行腳本的一部分(執行選擇)。當您在編寫腳本時,這通常很有用,因為您希望確認腳本的部分行為是否符合預期。

  • 要執行腳本的一部分,請突出顯示您想要執行的部分。
  • 點擊控制台右上角的 執行選擇,或按下鍵盤上的 F8。

這次,VS Code 僅在控制台窗格中執行選定的代碼行。

How to run only a part of a script

在 PowerShell ISE 中運行腳本

在 PowerShell ISE 中運行腳本的方式非常相似。

  • 啟動 PowerShell ISE。
  • 使用 檔案 > 開啟,或從工具欄中選擇 開啟 圖標,或使用鍵盤快捷鍵 Ctrl + O,從之前的演示中打開腳本(在此例中 – C:\TEMP\MyPowerShellISEScript.ps1)。
  • 選擇腳本文件並點擊 開啟

要執行整個腳本,請使用工具欄上的 執行 按鈕或按下鍵盤上的 F5。這將在控制台窗格中執行腳本文件,並返回結果。

Running a script in the PowerShell ISE

如果您只需要執行腳本的一部分,請突出顯示該部分並從工具欄中選擇運行選擇,或者在鍵盤上按F8。同樣,PowerShell ISE只會執行選定的代碼行。

You can also execute only part of the script

從命令提示符運行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/