
C++ 開發環境
Windows PowerShell 簡易教學
Windows PowerShell 是 Microsoft 提供的新版命令列, PowerShell 同時也是物件導向的指令碼語言, Windows PowerShell 直接就是 PowerShell 的直譯器。以下分為兩個方面介紹 Windows PowerShell
- 簡單程式
- 常用指令
簡單程式
Windows PowerShell 可以直接在其上寫簡單程式,以下整理常用的運算子與控制結構。下表整理常用運算子
運算子 | 符號 |
---|---|
算術運算 | +, -, *, /, % |
指派運算 | =, +=, -=, *=, /=, %= |
邏輯運算 | !, -not, -and, -or |
比較運算 | -eq, -ne, -gt, -ge, -lt, -le, -contains |
下表整理常用控制結構
控制結構 | 關鍵字 |
---|---|
迴圈 | foreach |
迴圈 | for |
迴圈 | while |
迴圈 | do, while |
迴圈 | do, until |
選擇 | if, elseif, else |
選擇 | switch |
以下示範算術運算子,包括加 + ,減 - ,乘 * ,除 / ,取餘數 %

指派運算子包括指派 = ,相加然後指派 += ,相減然後指派 += ,相乘然後指派 *= ,相除然後指派 /= ,取餘數然後指派 %=

邏輯運算子包括非 ! 與 -not ,且 -and ,或 -not

比較運算子包括比較相等 -eq ,不相等 -ne ,大於 -gt , 大於等於 -ge ,小於 -lt ,小於等於 -le ,包含 -contains

1..100 表示產生整數 1 到 100 的陣列。
以下示範 foreach 迴圈

以下示範 for 迴圈

以下示範 while 迴圈

以下示範 do-while 迴圈, while 之後的條件為真就會持續進行迴圈

以下示範 do-until 迴圈, until 之後的條件為真就會結束迴圈

以下示範 if-else 選擇陳述

以下示範 switch 選擇陳述

常用指令
Get-Date 指令用來取得現在的日期與時間

這些指令的第一個英文字母雖然是大小,但也可以用全小寫 get-date ,以下指令相同。
Set-Location 指令用來切換路徑,加上空格與 .. 可以回到上一層路徑,別名有 sl 、 cd 、 chdir

Get-ChildItem 指令用來印出當前路徑下的檔案目錄,別名有 gci 、 ls 、 dir

Write-Output 指令用來輸出其後參數到命令列上,別名有 write 、 echo

Clear-Host 指令用來清空命令列上印出的資訊,使游標恢復到第一行,別名有 cls 、 clear

Get-Command 指令用來印出所有 PowerShell 的指令

Get-Help 指令用來印出指令的語法、別名

