Get started with the zero---powershell app (a full example of getting started)

Source: Internet
Author: User
Tags aliases

Learn a technology, not only to, but also to use, examples are the best tool to get you started quickly. This article is to use examples, no, a lot of examples to take you into the PowerShell application world.

This article mainly introduces some of the basic knowledge of PowerShell introduction, the technology of small Bailai said can be quick to get started, the technical veteran can review the consolidation, nonsense not much to say, directly into the topic.

PowerShell, I believe you are already familiar with, in the previous article to introduce the PowerShell development process, about PowerShell more introduction, we can also refer to here: https://msdn.microsoft.com/ Powershell/scripting/getting-started/fundamental/using-windows-powershell.

1. How to start PowerShell

PowerShell is generally integrated in Windows systems today, with the following startup methods:

A) Windows PowerShell, Windows PowerShell--, all Programs, direct Start menu (different systems may not have the same path). The effect after startup is as follows:

b) Start menu, run, enter PowerShell. After the startup succeeds and the same.

c) PowerShell, command Prompt (Prompt). The effect after startup is as follows:

After a successful start, you can enter the command you want to execute, which is the interactive interface of PowerShell.

2. How to run programs, scripts, and existing software

If you used to develop some other tools, you might have executable files, Perl scripts, batch files, and so on, and if you don't want to give them up, you can run them directly using PowerShell.

A) Run the program, script, batch file, or other executable file under the system path, and you can enter the file name directly. For example, if I put a test.cmd file under "C:\Windows\System32", I can run it as follows:

b) If there are spaces in the file name or path, you need to use the (') quotation mark to expand the command and precede the symbol (&), which is called the call operation:

c) To run the command under the current directory, you need the file name plus. \, such as:

D) To run the commands in the current directory, and the command name or path with a space, you need to add both the symbol (&) and ('):

3. Run the PowerShell command

In addition to supporting traditional Windows executables, PowerShell has launched a powerful new command called a cmdlet. All cmdlet command rules follow the grammatical structure of the verb-noun, such as get-command, Get-content, and so on, the following Get-command command means to get all the command collections that contain the process:

When you enter "Get-command-name *process", you do not have to enter the full command name, PowerShell will help you use the TAB key to automatically complete the command input, including the name of the command and the name of the parameter, such as input can be:get-comm< Tab>-n<tab> *process.

4. How to find the specified command and command details

A) to perform a task in PowerShell without knowing what command to use, look for a command that can use the Get-command command, and it supports wildcard characters. Examples are as follows:

b) To see the role of a command, you can use the Get-help command. As an example, you can also specify parameters-detailed,-full,-examples.

c) Because PowerShell can use objects in the. NET framework, you can use the Get-member command to view the properties and methods of an object. Examples are as follows:

5. How to invoke the PowerShell script externally

PowerShell scripts may sometimes need to be called from batch files, scheduled tasks, or other non-PowerShell programs, Syntax: PowerShell ' & ' full path of the script ' arguments '. The following example calls PowerShell from CMD.

6. How to know the status of the last command executed

PowerShell provides two variables to detect the success of the last command executed: $lastExitCode and $?.

$lastExitCode: A numeric variable that returns the exit code or error level returned by the last script or application execution:

$?: Boolean variable that returns the success or failure of the last command executed:

Variable $? Use a more general way to describe the state of the last application exit, and PowerShell sets the $? to False when the following application error occurs:

    • Application exit code not 0;
    • Cmdlet or script output error message;
    • A cmdlet or script captures a terminating error or exception.

When the command executes without errors, PowerShell sets the $? variable to True.

7. How to calculate command execution time

If you want to calculate a command execution time, you can use the Measure-command command, as follows:

8. Understanding the alias of a command

PowerShell some of the built-in commands have aliases, easy to remember and input, you can use the GET-HELP command to view aliases, the following Get-children command has three aliases: GCI, ls and dir, enter any one can enumerate the current directory.

9. How to operate using the Admin Console history command

In the PowerShell window, press the UP and DOWN ARROW keys to find the history command to call, or you can run the get-history command lookup, which is called with the Invoke-history ID:

10. How to redirect output

If you want to output the resulting results to a file, you can use the Out-file command or the redirect operator to save the results of the command output in a file:

11. How to record the full text of a PowerShell session

If you want to generate a record of the current session, you can run the Start-transcript command, which is based on the current system time. If you want to stop, run Stop-transcript:

12. How to display object properties as a list or table

Use the format-list and format-table commands to see examples directly:

Note:

    • All of the commands described in this article can be used get-help to get more information, to learn more about it before you can use it better (learning to use PowerShell Get-help/get-command/get-member is the three most common commands to learn how to make the most of it).
    • The basic use of Windows PowerShell is completely not limited to the above examples, learning these, just help get started, you can better continue to learn.
    • In the future there will be more detailed updates, please look forward to.

[Original articles, reproduced please indicate the source, only for study purposes, if there are errors please leave a message, such as feel good, please recommend, thank you support]

[Original: Http://www.cnblogs.com/lavender000/p/6935589.html, from forever-smoked]

Get started with the zero---powershell app (a full example of getting started)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.