Windows PowerShell

From Wikipedia, the free encyclopedia

Windows PowerShell

Screenshot of a sample PowerShell session
Developer: Microsoft Corporation
Latest release: 1.0 / November 14, 2006
OS: Windows XP, Windows Server 2003, Windows Vista, Windows Server "Longhorn"
Use: Operating system shell
License: MS-EULA
Website: Windows PowerShell

Windows PowerShell, previously Microsoft Shell or MSH (codenamed Monad) is an extensible command line interface (CLI) shell and scripting language product developed by Microsoft. The product is based on object-oriented programming and the Microsoft .NET Framework.

Windows PowerShell 1.0 is available for download from the Microsoft web site.

Microsoft originally intended to launch PowerShell along with Windows Vista, but later defined a separate release schedule for PowerShell. Windows PowerShell is the foundation of the administrative experience for Microsoft Exchange Server 2007 where everything is done via command line interfaces and the administrative GUI is layered on top of those commands.

PowerShell requires version 2.0 of the .NET Framework, and is supported on Windows XP, Windows Server 2003, and Windows Vista, and is available in x86, x64 and Itanium editions.

Contents

[edit] History

Every released version of Microsoft DOS and Microsoft Windows for personal computers has featured a command line interface tool. These have been COMMAND.COM (in installations relying on MS-DOS) and cmd.exe (in Windows NT-based installations). These were not always capable of reproducing or automating all of the administrative functions available in the graphical user interface (GUI), although under Windows Server 2003 this situation was improved. This has been due to limitations within the command-line equivalents or to Microsoft not providing full fidelity command-line equivalents.

Additionally, in contrast to UNIX shells, neither command.com nor cmd.exe is POSIX.2 conformant. Microsoft's Services for UNIX does include a POSIX.2-conformant shell (pdksh) but this and other UNIX shells are still incapable of performing many routine tasks natively done through the Windows GUI. While the Services for UNIX suite is now built into Windows Server 2003 R2, and is now included with some editions of Windows Vista, it was previously a for-sale add-on, which posed a barrier to adoption.

Users have also been hindered by a lack of adequate documentation of command-line functionality. In contrast, many GUI applications in UNIX are merely wrappers for documented command-line tools.

Microsoft attempted to address some of these shortcomings by introducing the Windows Script Host in 1998 with Windows 98. The Windows Script Host was a new scripting layer which could implement a variety of scripting languages to control applications. However, the Script Host had its own deficiencies. It was not integrated with the shell, its documentation was not very accessible, and it quickly gained a reputation as a system vulnerability after several high-profile computer viruses exploited substantial weaknesses in its security provisions.

Windows Server 2003 and certain versions of Windows XP included a command-line-based script host called CScript.exe (essentially just a portion of the Windows Script Host introduced earlier), but it was not integrated into the existing shell (cmd.exe). These operating systems provided other ad hoc CLIs as well (netsh, for example), which were not fully integrated either.

A fundamental limitation with Windows' automation was the fact that its native administrative interfaces were open to Microsoft's proprietary GUI-based tools, and any scriptable interfaces Microsoft chose to provide. The GUI was the primary interface, and it proved difficult to provide scriptable "wrappers"; this is exactly opposite to most UNIX implementations, where the scriptable command-line tool provided the base functionality, with any GUI simply layered on top of it.

Microsoft designed PowerShell to mitigate these problems — and to alleviate their onus of developing distinct GUI and CLI applications — by integrating a more secure scripting language into a new, extensible command shell capable of performing and automating the same core administrative tasks as graphical user interfaces typically have in Windows. Exchange Server 2007's management user interface, for example, is built on top of PowerShell. In most everyday tasks, PowerShell can be used in place of CMD.EXE and Windows Script Host (WSH). However, for backward-compatibility purposes both CMD.EXE and WSH will continue to be supported by Windows. Microsoft has committed to building future GUI tools on top of PowerShell, ensuring that the core administrative functionality remains scriptable, in a way reminiscent of the UNIX operating system.

Microsoft published the second "Monad" public beta release on September 11, 2005 and released Beta 3 on January 10, 2006.

Microsoft announced on April 25, 2006 that MSH was renamed to Windows PowerShell, positioning it as a significant part of their management technology offerings.[1] Release Candidate 1 of PowerShell was released at the same time.

Release Candidate 2 of PowerShell was released September 26, 2006.

Windows PowerShell v1.0 was released to web (RTW) November 14, 2006.

PowerShell for Vista was released on Jan 30, 2007.[2]

[edit] Central concepts

PowerShell's codename, Monad, comes from Gottfried Leibniz's monadology,[3] a philosophy in which the universe is a composite of fundamental elements called Monads that are integrated in a "pre-established harmony". Similarly, PowerShell is a composite of the complex tasks of a series of components. The components are special programs called cmdlets (pronounced command lets), which are .NET classes designed to use the features of the environment. The key difference between the usual UNIX approach and the PowerShell one is that rather than creating a "pipeline" based on textual input and output, PowerShell passes data between the various cmdlets as objects (structured data).

If accessed individually from the command line, a cmdlet's output will automatically be converted into text, but if its output is to be used by another cmdlet, it will be converted into whatever form of object is most appropriate for that cmdlet's input. This has the advantage of eliminating the need for the many text-processing utilities which are common in UNIX pipelines, such as grep and awk, as well as allowing things to be combined interactively, or in a scripting environment, which would otherwise require a more complex programming language. For instance, a listing of processes will consist not of text describing them, but objects representing them, so that methods can be called on those objects without explicit reference to any outside structure or library.

[edit] Features

The first version contains these notable features:

  • A C#-like scripting language with many power user features such as direct support for hash tables, switch statements which can test on regular expressions, array slicing and anonymous methods (script blocks) which can be stored as data and then later executed. That is in addition to the normal scripting language features such as looping (for/foreach/while), conditional statements (if/switch), variable scoping (global/script/local) and the abililty to define functions.
  • Cmdlets all inherit certain options, allowing the user to choose things such as the level of interaction and how to deal with errors. Cmdlets which produce side effects support the options -WhatIf and -Confirm. -WhatIf informs the user what would have happened, but no action takes place. -Confirm informs the user what is about to happen and allows the user to control whether it takes place or not.
  • One option for dealing with errors is to invoke a "suspend" feature which allows the user to enter a new command shell, investigate a problem, and resume the original command. A simple mechanism lets the programmer define the prompts to be shown in such circumstances.
  • An extensible provider model allows access to and manipulation of the file system and other hierarchical stores. Some examples: PowerShell comes with a registry provider which allows access to the registry via the "HKLM" and "HKCU" hives; with this, the registry can be browsed by executing commands like "dir HKLM:\SOFTWARE\Microsoft" at the shell prompt. PowerShell comes with providers for the certificate store, the environment, and shell functions and aliases. Like cmdlets, the provider model is extensible, allowing third parties to create their own provider model and plug it into PowerShell.
  • A concept called "execution policies" which helps to deliver a more secure command line administration experience. Execution policies define the restrictions under which PowerShell loads configuration files and runs scripts. The four execution policies are Restricted, AllSigned, RemoteSigned, and Unrestricted.
  • Support for the use of script signing to verify the identity of a script publisher and to validate the integrity of a published script using digital signatures.
  • The command-line options are generally whole words, but can be specified as the minimum number of letters necessary to disambiguate. For example, the option -show-detailed-information could be entered as -s if no other option begin with 's'.
  • Comprehensive, user-extensible tab completion features. The cmd.exe shell in current versions of Windows can only complete file or directory names, in contrast to the advanced completion in shells such as bash and zsh.
  • The ability to assign the output of a command to a variable, which will then be an object or array of objects inspectable in any way desired.

[edit] Examples

  • Stop all processes that begin with the letter "p":
 PS> get-process p* | stop-process
  • Find the processes that use more than 1000 MB of memory and kill them:
 PS> get-process | where { $_.WS -gt 1000MB } | stop-process
  • Calculate the number of bytes in the files in a directory:
 PS> get-childitem | measure-object -property length -sum
  • Determine whether a specific process is no longer running:
 PS> $processToWatch = get-process notepad
 PS> $processToWatch.WaitForExit()
  • Change the case of a string from lower to upper:
 PS> "hello, world!".ToUpper()
  • Insert the string "ABC" after the first character in the word "string" to have the result "sABCtring":
 PS> "string".Insert(1, "ABC")
  • Download a specific RSS feed and show the titles of the 8 most recent entries:
 PS> $rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"
 PS> $blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
 PS> $blog.rss.channel.item | select title -first 8
  • Sets $UserProfile to the value of the UserProfile environment variable
 PS> $UserProfile = $env:UserProfile

[edit] File extensions

  • PS1 - Windows PowerShell shell script
  • PS1XML - Windows PowerShell format and type definitions
  • PSC1 - Windows PowerShell console file

[edit] See also

[edit] References

  1. ^ Snover, Jeffrey (April 25, 2006). Windows PowerShell (Monad) Has Arrived. Windows PowerShell team blog. MSDN. Retrieved on April 26, 2006.
  2. ^ Snover, Jeffrey (November 15, 2006). Windows PowerShell : Windows PowerShell & Windows Vista. Windows PowerShell team blog. MSDN. Retrieved on January 26, 2007.
  3. ^ Snover, Jeffrey (March 19, 2007). Monad Manifesto – the Origin of Windows PowerShell. Windows PowerShell team blog. MSDN. Retrieved on March 24, 2007.

[edit] Books

[edit] External links

[edit] Official Websites

[edit] Windows PowerShell Team

[edit] 3rd Party Tools

  • PowerShell Analyzer - The premium GUI based powershell Host and IDE.
  • PrimalScript - Example of a commercial Windows PowerShell editing environment
  • PowerGUI Community - PowerGUI is an extensible graphical administrative console for managing systems based on Windows PowerShell.
  • PowerShell Remoting - a light-weighted server-client application which allow you securely connect to remote PowerShell host and run script interactively.

[edit] Users Groups and Communities

  • Powershell Live - Powershell Live - The Powershell User community with forums, chat, articles, blogs, editorial columns, downloads and the latest Powershell News.
  • Get-PSUGUK - UK PowerShell Users Group
  • The French Windows PowerShell Community - vous trouverez ici de quoi bien débuter en PowerShell lorsque l'on est Français. De nombreux tutoriaux et examples de scripts sont disponibles.