2011-01-12 121 views

回答

24

安裝和配置PowerShell並不困難,但有點棘手。有三個基本步驟:

  1. 安裝(如果需要)
  2. 啓用腳本執行(默認禁用)
  3. 編輯配置文件腳本(默認情況下丟失)

INSTALL

如果您有Windows Vista或Windows 7,則應已安裝PowerShell。如果您使用的是舊版本的Windows,或者因爲某些原因未安裝PowerShell,請轉至here,向下滾動到標有「Windows Management Framework Core(WinRM 2.0和Windows PowerShell 2.0)」的部分,然後單擊下載鏈接你的OS。如果您使用的是64位Windows XP,請使用Windows Server 2003版本。

啓用腳本

這是棘手的部分。腳本通常是禁用的(默認情況下,只允許在控制檯交互使用)。別擔心,你只需做一次:

查找Windows資源管理器快捷方式圖標 對PowerShell的(在Windows 7看在 「開始|所有程序|附件| 的Windows PowerShell」),右鍵點擊 它並選擇「以管理員身份運行」

PowerShell將打開一個提示(默認提示是PS>)。執行以下操作:

PS> Set-ExecutionPolicy RemoteSigned

離開外殼打開的最後一步。

編輯個人資料

在提示符下,這樣做:

PS> New-Item -Path $Profile -ItemType file -Force 
PS> notepad $Profile 
PS> exit 

保持記事本窗口中打開。

瞧!您已準備好開始學習PowerShell。您不再需要以管理員身份啓動PowerShell,只需更改執行策略即可。下次只需正常啓動。

獎金

以下內容粘貼到您的仍然打開記事本窗口:

Set-Alias rc Edit-PowershellProfile 

function Prompt 
{ 
    $mywd = (Get-Location).Path 
    $mywd = $mywd.Replace($HOME, '~') 
    Write-Host "PS " -NoNewline -ForegroundColor DarkGreen 
    Write-Host ("" + $mywd + ">") -NoNewline -ForegroundColor Green 
    return " " 
} 

function Edit-PowershellProfile 
{ 
    notepad $Profile 
} 

保存,然後重新啓動PowerShell的正常進行。 PowerShell在啓動時運行此配置文件腳本(如果您熟悉bash,配置文件與.bashrc類似)。

現在您可以開始自定義。實際上,您可以鍵入rc以在記事本中打開您的配置文件。請記住保存對配置文件的更改,然後重新啓動PowerShell以重新執行它。

您現在已準備好破解這些書籍和教程,並開始編寫和運行PowerShell腳本。

享受!

+0

非常有幫助後感謝 - 這是驚人的,以我的微軟能有多難作出「我想安裝PowerShell 2」這樣簡單的事情 – jcollum 2012-03-28 23:08:20

2

從Windows Vista開始Powershell作爲操作系統的一部分包含在內,不需要安裝。只需在運行窗口中輸入「powershell.exe」,即可開始使用。

像大多數其他語言一樣,在你使它變得有用之前,可能需要一些基本的閱讀。但是如果你熟悉Perl或C#,它應該會非常快。

至於改變提示。這是通過定義一個名爲prompt的函數完成的。只需在PowerShell控制檯中鍵入以下內容並按回車鍵即可

function prompt() { "My Prompt :>" } 
0

我是管理員。

PS> Write-Output "" >> $Profile 
gave : 
" Could not find a part of the path 'H:\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'. 
At line:1 char:19 
+ Write-Output "" >> <<<< $Profile 
    + CategoryInfo   : OpenError: (:) [], DirectoryNotFoundException 
    + FullyQualifiedErrorId : FileOpenFailure " 

因此

PS> notepad $Profile 

了:

"The system cannot find the path specified." 
+0

謝謝,我已經更新了我的答案(它更詳細),但它應該適用於所有情況。 – jwfearn 2011-01-31 23:15:34