2011-01-12 68 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