How to install and configure PowerShell?
Before one can get started learning PowerShell, one needs to install it and configure it to run scripts.
What's an easy way to do this?
Does one really 开发者_StackOverflow中文版need to read several chapters in a book or tutorial before one can do something as simple as creating a script file, defining an alias, or changing the prompt?
Installing and configuring PowerShell is not hard but it's a little tricky. There are three basic steps:
- Install (if necessary)
- Enable script execution (disabled by default)
- Edit your profile script (missing by default)
INSTALL
If you have Windows Vista or Windows 7, PowerShell should be already installed. If you're on an older version of Windows or if PowerShell is not installed for some reason, go here, scroll down to the section labeled "Windows Management Framework Core (WinRM 2.0 and Windows PowerShell 2.0)" and click on the download link for your OS. If you're on 64-bit Windows XP, use the Windows Server 2003 version.
ENABLE SCRIPTING
This is the trickiest part. Scripting is usually disabled (only interactive use at the console is allowed by default). Don't worry, you only have do this once:
Find a Windows Explorer shortcut icon for PowerShell (on Windows 7 look in "Start | All Programs | Accessories | Windows PowerShell"), right-click on it and choose "Run as Administrator"
PowerShell will open an present a prompt (by default the prompt is PS>
). Do the following:
PS> Set-ExecutionPolicy RemoteSigned
Leave the shell open for the last step.
EDIT PROFILE
At the prompt, do this:
PS> New-Item -Path $Profile -ItemType file -Force
PS> notepad $Profile
PS> exit
Keep the notepad window open.
Voila! You're ready to start learning PowerShell. You no longer need to launch PowerShell as admin, that was only necessary to change the execution policy. Next time just launch it normally.
Bonus
Paste the following into your still open Notepad window:
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
}
Save, then relaunch PowerShell normally. PowerShell runs this profile script when it starts (If you're familiar with bash
, the profile is similar to .bashrc
).
Now you can start customizing. In fact, you can type rc
to open your profile in Notepad. Remember to save your changes to your profile and relaunch PowerShell to re-execute it.
You're now ready to crack open the books and tutorials and start writing and running PowerShell scripts.
Enjoy!
Starting with Windows Vista Powershell is included as part of the operating system and doesn't need to be installed. Simply type "powershell.exe" in the Run window and you are ready to go.
Like most other languages probably some basic reading will be necessary before you can make it useful. But if you're familiar with either Perl or C# it should go quite fast.
As for changing the prompt. It's done by defining a function named prompt
. Just type the following in a powershell console and hit enter
function prompt() { "My Prompt :>" }
精彩评论