確認 PowerShell 版本
PowerShell
$PSVersionTable.PSVersion
建議使用 PowerShell 7+。若版本低於 7,可執行
winget install Microsoft.PowerShell 升級。
設定執行策略(Execution Policy)
預設執行策略為
Restricted,不允許執行任何腳本。需修改才能執行 .ps1 檔案。
PowerShell(管理員)
# 查看當前執行策略
Get-ExecutionPolicy
# 設為 RemoteSigned(推薦:允許本地腳本,遠端需簽章)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# 或設為 Bypass(不限制,開發用)
Set-ExecutionPolicy Bypass -Scope CurrentUser
| 策略 | 說明 |
|---|---|
Restricted |
不允許任何腳本(預設) |
RemoteSigned |
本地腳本可執行,遠端需數位簽章(推薦) |
Bypass |
不限制,所有腳本均可執行 |
AllSigned |
所有腳本都需受信任的簽章 |
安裝常用工具
安裝 Git
winget install Git.Git
安裝 Node.js(LTS)
winget install OpenJS.NodeJS.LTS
安裝 VS Code
winget install Microsoft.VisualStudioCode
winget 是 Windows 內建的套件管理器,無需額外安裝。
常用 PowerShell 指令
| 用途 | PowerShell 指令 | 等同 CMD |
|---|---|---|
| 列出檔案 | Get-ChildItem |
dir |
| 切換目錄 | Set-Location C:\path |
cd C:\path |
| 顯示當前路徑 | Get-Location |
cd |
| 建立目錄 | New-Item -ItemType Dir |
mkdir |
| 複製檔案 | Copy-Item src dst |
copy |
| 移動/重命名 | Move-Item src dst |
move / ren |
| 刪除檔案 | Remove-Item file |
del |
| 刪除目錄 | Remove-Item dir -Recurse |
rmdir /s |
| 顯示檔案內容 | Get-Content file |
type |
| 搜尋字串 | Select-String "pattern" |
findstr |
| 環境變數(查) | $env:VAR_NAME |
echo %VAR% |
| 環境變數(設) | $env:VAR = "value" |
set VAR=value |
Git 基礎指令
初次設定
# 設定使用者名稱和信箱(必填)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 設定預設分支名稱
git config --global init.defaultBranch main
# 確認設定
git config --global --list
日常操作
git init # 初始化新 repo
git clone <url> # 複製遠端 repo
git status # 查看狀態
git add . # 加入所有變更
git commit -m "message" # 提交
git push origin main # 推送到遠端
git pull origin main # 拉取遠端更新
環境變數永久設定
寫入 User 作用域(推薦)
# 寫入
[Environment]::SetEnvironmentVariable("MY_VAR", "my_value", "User")
# 讀取
[Environment]::GetEnvironmentVariable("MY_VAR", "User")
# 刪除(設為空值)
[Environment]::SetEnvironmentVariable("MY_VAR", $null, "User")
使用
SetEnvironmentVariable 寫入後,需重新開啟 PowerShell 才能在新 Session 中讀取到。
驗證所有工具安裝
PowerShell
git --version
node --version
npm --version
code --version
winget --version
若所有指令都能正常輸出版本號,代表環境設定完成。