`

Windows PowerShell™ Getting Started Guide

阅读更多

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="width: 391.5pt; height: 27pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

 

Windows PowerShell™ Getting Started Guide

Microsoft Corporation

Published: September 2006

Abstract

Windows PowerShell™ is a new Windows command-line shell designed especially for system administrators. The shell includes an interactive prompt and a scripting environment that can be used independently or in combination.

This document is designed to introduce new users to Windows PowerShell and to acquaint them with its basic features. For more detailed information, see the Windows PowerShell Primer.

<shape id="_x0000_i1026" style="width: 391.5pt; height: 27pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image003.png"><font face="Arial" size="2"></font></imagedata></shape>

 

 


Contents

Windows PowerShell Getting Started Guide Copyright................................................... 5

Windows PowerShell Design Goals.............................................................................. 5

Primary Focus......................................................................................................... 6

Introducing Windows PowerShell.................................................................................. 6

Windows PowerShell Cmdlets................................................................................... 7

A New Scripting Language........................................................................................ 7

Windows Commands and Utilities............................................................................. 8

Processing Objects.................................................................................................. 8

Object Pipelines...................................................................................................... 9

Interaction and Scripting............................................................................................ 10

An Interactive Environment...................................................................................... 10

Support for Scripting............................................................................................... 10

Starting Windows PowerShell..................................................................................... 11

Using Windows PowerShell........................................................................................ 12

Get-Help: Getting Help............................................................................................ 12

Using Cmdlets....................................................................................................... 14

Learning About Objects: Get-Member...................................................................... 15

Using Cmdlet Parameters.................................................................................... 18

Common Parameters.......................................................................................... 18

Formatting Command Output.................................................................................. 19

Using Aliases........................................................................................................ 21

Creating an alias................................................................................................. 22

Deleting an alias................................................................................................. 22

Using Functions to Create Alternate Names.......................................................... 23

Using Windows Programs....................................................................................... 23

Managing Errors..................................................................................................... 24

Navigating Windows PowerShell................................................................................. 24

Navigating the File System...................................................................................... 25

Navigating the Registry........................................................................................... 25

Navigating the Certificate Store................................................................................ 27

Navigating Other Drives........................................................................................... 28

About Windows PowerShell Drives........................................................................... 28

Drives and Providers............................................................................................... 29

Customizing Windows PowerShell.............................................................................. 30

Examine the Execution Policy................................................................................. 30

Windows Powershell Profiles................................................................................... 30

Understanding the Profiles................................................................................... 31

Creating a Profile................................................................................................ 31


This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document.  Information in this document, including URL and other Internet Web site references, is subject to change without notice.  The entire risk of the use or the results from the use of this document remains with the user.  Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred.  Complying with all applicable copyright laws is the responsibility of the user.  Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document.  Except as expressly provided in any written license agreement from Microsoft; the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

© 2006 Microsoft Corporation.  All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, Windows 2000, Windows XP, Windows Server 2003, Windows Vista, .NET Framework 2.0, .NET Framework 2.0 Runtime Components, and Win32 are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.


Windows PowerShell Design Goals            

Windows PowerShell is a new Windows command-line shell designed especially for system administrators. The shell includes an interactive prompt and a scripting environment that can be used independently or in combination.

Unlike most shells, which accept and return text, Windows PowerShell is built on top of the .NET common language runtime (CLR) and the .NET Framework, and accepts and returns .NET objects. This fundamental change in the environment brings entirely new tools and methods to the management and configuration of Windows.

Windows PowerShell introduces the concept of a cmdlet (pronounced "command-let"), a simple, single-function command-line tool built into the shell. You can use each cmdlet separately, but their power is realized when you use these simple tools in combination to perform complex tasks. Windows PowerShell includes more than one hundred basic core cmdlets, and you can write your own cmdlets and share them with other users.

Like many shells, Windows PowerShell gives you access to the file system on the computer. In addition, Windows PowerShell providers enable you to access other data stores, such as the registry and the digital signature certificate stores, as easily as you access the file system.

This Getting Started guide provides an introduction to Windows PowerShell: the language, the cmdlets, the providers, and the use of objects.

Primary Focus

The primary focus of this document is to help Windows PowerShell users get started with Windows PowerShell. This document describes the features of the shell that you need to start using the shell. For a detailed examination of the shell, its features, and examples of how to use the shell, see the Windows PowerShell Primer.


Introducing Windows PowerShell            

Most shells, including Cmd.exe and the SH, KSH, CSH, and BASH Unix shells, operate by executing a command or utility in a new process, and presenting the results to the user as text. Over the years, many text processing utilities, such as sed, AWK, and PERL, have evolved to support this interaction.

 These shells also have commands that are built into the shell and run in the shell process, such as the typeset command in KSH and the dir command in Cmd.exe.  In most shells, because there are few built-in commands.many utilities have been created.

Windows PowerShell is very different. 

·      Windows PowerShell does not process text. Instead, it processes objects based on the .NET platform. 

·      Windows PowerShell comes with a large set of built-in commands with a consistent interface.

·      All shell commands use the same command parser, instead of different parsers for each tool. This makes it much easier to learn how to use each command.

Best of all, you don't have to give up the tools that you have become accustomed to using. You can still use the traditional Windows tools, such as Net, SC, and Reg.exe in Windows PowerShell.

Windows PowerShell Cmdlets

A cmdlet (pronounced "command-let") is a single-feature command that manipulates objects in Windows PowerShell. You can recognize cmdlets by their name format -- a verb and noun separated by a dash (-), such as Get-Help, Get-Process, and Start-Service.

In traditional shells, the commands are executable programs that range from the very simple (such as attrib.exe) to the very complex (such as netsh.exe).

In Windows PowerShell, most cmdlets are very simple, and they are designed to be used in combination with other cmdlets. For example, the "get" cmdlets only retrieve data, the "set" cmdlets only establish or change data, the "format" cmdlets only format data, and the "out" cmdlets only direct the output to a specified destination.

Each cmdlet has a help file that you can access by typing:

get-help <cmdlet-name> -detailed

The detailed view of the cmdlet help file includes a description of the cmdlet, the command syntax, descriptions of the parameters, and example that demonstrate use of the cmdlet.

A New Scripting Language

Windows PowerShell uses its own language, rather than reusing existing an languages, for the following reasons.

·      Windows PowerShell needed a language for managing.NET objects,

·      The language needed to provide a consistent environment for using cmdlets.

·      The language needed to support complex tasks, without making simple tasks more complex.

·      The language needed to be consistent with higher-level languages used in .NET programming, such as C#.

Windows Commands and Utilities

You can run Windows command-line programs in Windows PowerShell, and you can start Windows programs that have a graphic user interface, such as Notepad and Calculator, within the shell. You can also capture the text that programs generate and use that text in the ,shell, in much the same way you would in Cmd.exe.

Processing Objects

Although you might not realize it at first. when you work in Windows PowerShell, you are working with .NET objects. As you gain experience, the power of object processing becomes more evident, and you'll find yourself using the objects and even thinking in objects.

Technically, a .NET object is an instance of a .NET class that consists of data and the operations associated with that data. But you can think of an object as a data entity that has properties, which are like characteristics, and methods, which are actions that you can perform on the object.

For example, when you get a service in Windows PowerShell, you are really getting an object that represents the service. When you view information about a service, you are viewing the properties of its service object. And, when you start a service, that is, when you change the Status property of the service to "started," you are using a method of the service object.

All objects of the same type have the same properties and methods, but each instance of an object can have different values for the properties. For example, every service object has a Name and Status property. However, each service can have a different name and a different status.

When you're ready, it's easy to learn about the objects. To find out what type of object a cmdlet is getting, use a pipeline operator (|) to sent the results of a "get" command to the Get-Member command. For example, the following command sends the objects retrieved by a Get-Service command to Get-Member.

get-service | get-member

 

Get-Member displays information about the service object, including the typename of the object and a list of its properties and methods.

   TypeName: System.ServiceProcess.ServiceController

 

Name                      MemberType    Definition

----                      ----------    ----------

Name                      AliasProperty Name = ServiceName

add_Disposed              Method        System.Void add_Disposed(EventHandler value)

Close                     Method        System.Void Close()

Continue                  Method        System.Void Continue()

...

 

For information about the object class, copy and paste the typename, such as System.ServiceProcess.ServiceController, in MSDN. When you find the class, you can read the MSDN subtopics to learn about the properties and methods of objects based on that class, like the ones in Windows PowerShell.

To find the values of all of the properties of a particular object, use a pipeline operator (|) to send the results of a "get" command to a Format-List or Format-Table command. Use the Property parameter of the format cmdlets with a value of all (*). For example, to find all of the properties of the Schedule service on the system, type:

get-service schedule | format-list -property *

 

The following shows an example of the result.

Name                : Schedule

CanPauseAndContinue : True

CanShutdown         : True

CanStop             : True

DisplayName         : Task Scheduler

DependentServices   : {}

MachineName         : .

ServiceName         : Schedule

ServicesDependedOn  : {RpcSs}

ServiceHandle       : SafeServiceHandle

Status              : Running

ServiceType         : Win32ShareProcess

Site                :

Container           :

 

You don't need to understand anything about objects when you are first learning Windows PowerShell, but keep the concept in the back of your mind. You'll soon be able to use the objects to their best advantage.

Object Pipelines

One major advantage of using objects is that it makes it much easier to pipeline command, that is, to pass the output of one command to another command as input. The communication often requires string manipulation to convert output from one format to another and to remove titles and column headings. 

Windows PowerShell  provides a new interactive model that is based on objects, rather than text.  The cmdlet that receives an object can act directly on its properties and methods without any conversion or manipulation. Users can refer to properties and methods of the object by name, rather than calculating the position of the data in the output.

In the following example, the result of an IpConfig command is passed to a Findstr command. The pipeline operator (|) sends the result of the command on its left to the command on its right. In Microsoft® Windows® PowerShell, you don't need to manipulate strings or calculate data offsets.

PS> ipconfig | findstr "Address"

        IP Address. . . . . . . . . . . . : 172.28.21.5

        IP Address. . . . . . . . . . . . : 172.30.160.225

 

Interaction and Scripting            

An Interactive Environment

Like other shells, Windows PowerShell supports a complete interactive environment. When you type a command at the prompt, the command is processed and the output is displayed in the shell window. You can send the output of a command to a file or printer, or you can use the pipeline operator (|) to send the output to another command.

Support for Scripting

If you run particular commands or command sequences repeatedly, or if you develop a series of commands to perform a complex task, you will want to save your commands in a file and execute the command file, instead of typing commands at the prompt. A file of commands is called a script.

In addition to its interactive interface, Windows PowerShell fully supports scripting. In Windows PowerShell, script files have a .ps1 file name extension. To run a script, type the name of the script at the command prompt. The file name extension is optional.

For example:

c:\test\testscript.ps1

 

or

c:\test\testscript

 

You must specify the fully qualified path to the script file, even if the script is in the current directory. To indicate the current directory, type the directory name or use a dot (.) to represent the current directory. For example:

.\testscript.ps1

 

Although scripts are extremely useful -- even essential -- in some enterprises, they can be used to spread malicious code. As a result, the security policy in Windows PowerShell, called an execution policy, lets you determine whether scripts can run and whether they must include a digital signature. To eliminate an obvious risk, none of the execution policies in Windows PowerShell allow you to run a script by double-clicking its icon. For more information, type:

get-help about_signing

 

Windows PowerShell also includes a very rich scripting language that allows you to create scripts from the simplest to the very complex. It supports language constructs for looping, conditions, flow-control, and variable assignment.

Starting Windows PowerShell            

To start Windows PowerShell from the Start menu, click Start, click All Programs, click Windows PowerShell 1.0, and then click Windows PowerShell.

 

To start Windows PowerShell from the Run box, click Start, click Run, and type:

powershell

 

To start Windows PowerShell from a Command Prompt (cmd.exe) windows, at the command prompt, type:

powershell

 

To see the options for starting Windows PowerShell, in a Command Prompt window,

type:

powershell -?

 

When Windows PowerShell is open, you can use the Get-Help cmdlet to find help. At the Windows PowerShell command prompt, type:

get-help

Using Windows PowerShell            

This section explains the basics of using Windows PowerShell. It begins with the Get-Help cmdlet, which displays information about cmdlets and conceptual topics in Windows PowerShell. Then, it describes a few of the basic cmdlets, explains how to use cmdlet parameters, and shows you how to format the output of the cmdlets to get the data that you need in a useful display. The final topics explain how to use aliases to make it easier to work in Windows PowerShell, how to run traditional Windows programs in Windows PowerShell, and how to manage errors.

Get-Help: Getting Help

The Get-Help cmdlet is a useful tool for learning about Windows PowerShell. By reading the descriptions of the cmdlets, learning about the concepts, and exploring the language topics, you can begin to understand how to use Windows PowerShell .

The first topic of interest might be the help system. To display information about the help system in Windows PowerShell, type:

border-right: silver 1.5pt solid
分享到:
评论

相关推荐

    Windows PowerShell Getting Started Guide

    **Windows PowerShell 概述** Windows PowerShell 是微软推出的一款专为系统管理员设计的全新命令行外壳程序。它结合了交互式提示符和脚本环境,两者既可以独立使用,也可以协同工作。此指南旨在帮助新用户入门...

    PowerShell中文资料

    在学习PowerShell时,`gettingstarted.pdf`可能是入门教程,讲解如何开始使用PowerShell,包括安装、界面介绍、基本命令的使用以及如何逐步熟悉这个环境。`userguide.pdf`可能是用户指南,包含更详细的操作步骤和...

    官方中文教程power-shell

    `gettingstarted.pdf`可能是入门指南,涵盖了PowerShell的基础知识。在这个文档中,你可能学习到以下内容: 1. PowerShell界面:了解PowerShell ISE(集成脚本环境)或新的PowerShell 7的终端界面,以及如何启动和...

    PowerShellPack

    About_alias.help.txt About_arithmetic_operators.help.txt About_array.help.txt About_assignment_operators.help.txt About_associative_array.help.txt ...Gettingstarted.rtf Userguide.rtf Quadfold.rtf

    Professional Sitecore 8 Development

    Getting started with Sitecore development Front-end development techniques Incorporating design patterns into your Sitecore solutions Unit testing Sitecore applications Programming Sitecore’s ...

    缺少的clojure简介:缺少的clojure简介

    本文将深入探讨Clojure的基础知识,包括其核心特性、语法结构以及如何在Windows环境中进行配置和使用。 首先,Clojure的设计理念是简洁和表达力强。它的语法特点在于使用圆括号表示表达式,这使得代码具有很高的...

Global site tag (gtag.js) - Google Analytics