PowerShell Basic Tutorial (2) - Preliminary Use of Windows PowerShell

PowerShell Basic Tutorial (2) - Preliminary Use of Windows PowerShell

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

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

powershell
 
To start Windows PowerShell from a Command Prompt (cmd.exe) window, at the Command Prompt, type:

powershell
 
To see 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 introduces the basics of using Windows PowerShell. It begins with an introduction to the Get-Help cmdlet, which displays information about cmdlets and conceptual topics in Windows PowerShell. It then describes a few basic cmdlets, shows how to use cmdlet parameters, and then shows how to format the cmdlet output to get the data you need in a useful display. The final topics explain how to use aliases to make using Windows PowerShell easier, how to run traditional Windows programs in Windows PowerShell, and how to manage errors.

Get-Help: Get Help
The Get-Help cmdlet is a useful tool for learning about Windows PowerShell. By reading cmdlet descriptions, learning about concepts, and exploring language topics, you can start learning 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:

get-help
 
Then you might be interested in learning about a few basic cmdlets such as Get-Help, Get-Command, Get-Process, Get-Service, and Get-Eventlog. 

To display the simplest view of a cmdlet's help, type "get-help" followed by the name of the cmdlet. For example, to get help for the Get-Command cmdlet, type:

get-help get-command
 
If the cmdlet help is formatted incorrectly (that is, if it begins with an XMLNS tag), it is possible that Windows PowerShell execution policies on the system prevent the system from loading the configuration file used to format the cmdlet help. For information about enforcement policies, type:

get-help about_signing   
 
To display detailed help for a cmdlet, including parameter descriptions and examples, use the Detailed parameter of Get-Help. For example, for detailed help on the Get-Command cmdlet, type:

get-help get-command -detailed
 

To display all available help for a cmdlet, including technical information about the cmdlet and its parameters, use the Full parameter. For example, to get complete help for the Get-Command cmdlet, type:

get-help get-command -full
 
can also display the selected part of the help file. To see examples only, use the Examples parameter. For example, to display an example of the Get-Command cmdlet, type:

get-help get-command -examples
 
To see detailed parameter descriptions only, use the Parameter parameter of Get-Help. You can specify the name of the parameter, or use wildcards (*) to specify all parameters. For example, to see a description of the TotalCount parameter of Get-Command, type:

get-help get-command -parameter totalcount
 
To see all parameters for the Get-Command cmdlet, type:

get-help get-command -parameter *
 
You can also use one of the Windows PowerShell functions that call Get-Help. The Help function displays help content one full screen at a time. The Man function displays help similar to man pages in Unix. To display Help for the Get-Command cmdlet using the Help and Man functions, type:

man get-command
 

or

help get-command
 

When a specific help topic is requested, Get-Help displays the contents of that topic. But when wildcards are used to request multiple topics, Get-Help will display a list of topics. For example, to see a list of help topics for the "Get" cmdlet, type:

get-help get-*

Help about concepts in Windows PowerShell begins with "about_". To display help on a Windows PowerShell concept, type "get-help" followed by the name of the concept. For example, for help with wildcards, type:

get-help about_wildcard
 

<o:p> </o:p> To display a list of all conceptual help topics in Windows PowerShell, type:

get-help about_*
 

By reading the Help topics and trying the examples, you'll learn how Windows PowerShell works and how you can use it in your work.

Using Cmdlets 
A cmdlet (pronounced "command-let") is a simple, single-function command-line tool built into the shell. You can use cmdlets just like traditional commands and utilities. Start by typing the name of the cmdlet at the Windows PowerShell command prompt. Windows PowerShell commands are case-insensitive, so they can be typed in any case.

For example, try the Get-Date cmdlet:

C:\PS> get-date

Thursday, November 10, 2025 at 4:43:50 pm
 

<o:p> </o:p> To list cmdlets in a session, use the Get-Command cmdlet without any command parameters.

PS> get-command

<o:p> </o:p>

CommandType     Name                            Definition

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

Cmdlet          Add-Content                     Add-Content [-Path] <String[...

Cmdlet          Add-History                     Add-History [[-InputObject] ...

Cmdlet          Add-Member                      Add-Member [-MemberType] <PS...

...


 

The default Get-Command display has the following three columns: CommandType, Name, and Definition. When listing cmdlets, the Definition column shows the cmdlet's syntax. An ellipsis (…) in the syntax indicates that the data is truncated. 

The Get-Command cmdlet also gets commands and command elements other than cmdlets, including aliases (command nicknames), functions, and executables available in Windows PowerShell. 

The following command lists executable files available in Windows PowerShell by using the Name parameter of Get-Command.

PS> get-command *.exe

<o:p> </o:p>

CommandType Name                   Definition

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

Application 000StTHK.exe           C:\WINDOWS\system32\000StTHK.exe

Application 00THotkey.exe          C:\WINDOWS\system32\00THotkey.exe

Application accwiz.exe             C:\WINDOWS\system32\accwiz.exe

...
 

When listing executables, the Definition column contains the full path to the executable.

Then, try some of the other cmdlets, such as Get-Process, Get-Service, Get-EventLog, and Get-Alias. 

If you're already familiar with the simple "Get-" cmdlets, try the more interesting cmdlets, such as Get-WmiObject. This cmdlet is useful because it allows you to view and change components of a remote computer. For example, the following command gets information about the BIOS on the remote computer Server01:

get-wmiobject win32_bios -computername server01
 

If you need help with any cmdlet, type:

get-help <cmdlet 名称> -detailed

For example:

get-help get-alias -detailed。

Understanding Objects: Get-Member
One of the most useful cmdlets is Get-Member, which displays information about the .NET object returned by the command. This information includes the object's type, properties, and methods.

To use Get-Member, use the pipeline operator (|) to send the command results to Get-Member. For example:

get-service | get-member

This command shows that Get-Service actually returns a set of System.ServiceProcess.ServiceController objects -- one for each service on the computer.

TypeName:System.ServiceProcess.ServiceController

<o:p> </o:p>

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()

CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)

Dispose                   Method        System.Void Dispose()

Equals                    Method        System.Boolean Equals(Object obj)

ExecuteCommand            Method        System.Void ExecuteCommand(Int32 command)

get_CanPauseAndContinue   Method        System.Boolean get_CanPauseAndContinue()

get_CanShutdown           Method        System.Boolean get_CanShutdown()

get_CanStop               Method        System.Boolean get_CanStop()

get_Container             Method        System.ComponentModel.IContainer get_Container()

get_DependentServices     Method        System.ServiceProcess.ServiceController[] get_DependentServices()

get_DisplayName           Method        System.String get_DisplayName()

get_MachineName           Method        System.String get_MachineName()

get_ServiceHandle         Method        System.Runtime.InteropServices.SafeHandle get_ServiceHandle()

get_ServiceName           Method        System.String get_ServiceName()

get_ServicesDependedOn    Method        System.ServiceProcess.ServiceController[] get_ServicesDependedOn()

get_ServiceType           Method        System.ServiceProcess.ServiceType get_ServiceType()

get_Site                  Method        System.ComponentModel.ISite get_Site()

get_Status                Method        System.ServiceProcess.ServiceControllerStatus get_Status()

GetHashCode               Method        System.Int32 GetHashCode()

GetLifetimeService        Method        System.Object GetLifetimeService()

GetType                   Method        System.Type GetType()

InitializeLifetimeService Method        System.Object InitializeLifetimeService()

Pause                     Method        System.Void Pause()

Refresh                   Method        System.Void Refresh()

remove_Disposed           Method        System.Void remove_Disposed(EventHandler value)

set_DisplayName           Method        System.Void set_DisplayName(String value)

set_MachineName           Method        System.Void set_MachineName(String value)

set_ServiceName           Method        System.Void set_ServiceName(String value)

set_Site                  Method        System.Void set_Site(ISite value)

Start                     Method        System.Void Start(), System.Void Start(String[] args)

Stop                      Method        System.Void Stop()

ToString                  Method        System.String ToString()

WaitForStatus             Method        System.Void WaitForStatus(ServiceControllerStatus desiredStatus), System.Voi...

CanPauseAndContinue       Property      System.Boolean CanPauseAndContinue {get;}

CanShutdown               Property      System.Boolean CanShutdown {get;}

CanStop                   Property      System.Boolean CanStop {get;}

Container                 Property      System.ComponentModel.IContainer Container {get;}

DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}

DisplayName               Property      System.String DisplayName {get;set;}

MachineName               Property      System.String MachineName {get;set;}

ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}

ServiceName               Property      System.String ServiceName {get;set;}

ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}

ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}

Site                      Property      System.ComponentModel.ISite Site {get;set;}

Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}
 

This information may seem technical, but it's actually quite useful.
 

Guess you like

Origin blog.csdn.net/gggwfn1982/article/details/131015455