Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows 7 內建 PowerShell 2.0 深度解析:從指令腳本到系統管理的王者

編輯:Windows 7 系統介紹

對進階用戶,華麗 GUI 不如鍵盤指令高效。Win 7 內建 PowerShell 2.0,將幾乎所有 GUI 管理任務(控制系統、存取登錄、事件日誌、數位憑證等)轉化為可程式化的指令與腳本,堪稱命令列工具之王。

一、PowerShell 是什麼?

  • CMD 進化版,語法類 C#,提供 200+ 指令、迴圈、條件、流程控制、變數指派等程式結構;
  • 支援互動模式(逐行輸入)與腳本檔(.ps1)批次執行;
  • 結果可輸出至視窗、檔案、印表機,支援管線 | 串接指令;
  • 物件導向:指令傳遞的是 .NET 物件而非純文字,操作更強大。

二、啟動方式

  • 「開始→所有程式→附件→Windows PowerShell」:
    – Windows PowerShell:類 CMD 的命令列介面(提示符 PS 開頭);
    – Windows PowerShell ISE:圖形化整合腳本環境(編輯/除錯/互動控制台三合一),適合腳本開發。
  • 傳統 CMD 中輸入 powershell 亦可進入。

三、常用指令探索

  • Get-Command:列出所有指令(Alias/Cmdlet/Function),Cmdlet 129 條,格式 動詞-名詞(如 Get-Process、Start-Service);
  • 管線應用:Get-Process | Findstr "svchost" 只列含 svchost 的進程;
  • Get-Help Get-Process 查看詳細用法。

四、實用管理範例

功能指令範例
數學運算PS C:\> (2+3*4)/512
輸出文字PS C:\> "hello"
大小寫轉換PS C:\> "hello".ToUpper()
插入字串PS C:\> "hello".Insert(1, "my")
結果存檔PS C:\> (1-2)*3/4 > c:\lbb.txt
讀取檔案PS C:\> type c:\lbb.txt
變數運算PS C:\> $x=(3-1)*2; $x/3
陣列操作PS C:\> $dirs=dir; $dirs[0]
存取登錄PS C:\> cd hklm:; dir
存取憑證存放區PS C:\> cd cert:; dir
等待程式結束PS C:\> $p=get-process calc; $p.WaitForExit()
停止所有 i* 開頭進程PS C:\> get-process i* | stop-process
計算目錄下所有檔案大小總和PS C:\> get-childitem | measure-object -property length -sum

五、別名機制

Set-Alias gp Get-Process 讓 gp 成為 Get-Process 別名;Set-Alias ca calc 讓 ca 啟動計算機。極大提升互動效率。

XP/Vista/2003/2008 用戶可至 https://support.microsoft.com/kb/968929 下載安裝 PowerShell 2.0 體驗新世代命令列威力。

Copyright © Windows教程網 All Rights Reserved