Introduction to Variable basics in PowerShell _powershell

Source: Internet
Author: User

This article describes how to use variables in PowerShell. Variables can store the number of programs, such as developer assignments, command execution results, and so on.

Variables to do, do not need me to say more, wrote the program of the brothers know: If there is no variable in the program, it really can not let the program! There are variables in the batch processing, and the PowerShell of nature is not.

1, the variables in PowerShell are objects

Variables in PowerShell are based on the. NET Framework, so the variables in PowerShell are the same as those in. NET: They are objects, all objects!

Copy Code code as follows:
$i =1
$a =1,2,3
$list =dir D:\

The above Hongo defines three variables, namely $i, $a, $list. $i is an integer, $a is an array of integers, $list is more complex, it is the result of the dir command, is a more complex object. But in PowerShell, these three variables are objects!

2, the name of the PowerShell variable

The name of a variable is very simple, unlike the command of a variable in a general C # language. It should be noted, however, that in PowerShell, the variable must precede it with a "$" symbol to indicate that it is a variable. PHP is also used to identify variables with the $, do not know whether the creators of the two languages have any relationship, hehe.

3, the assignment of PowerShell variable

Variable assignment values are "=" can be used. In PowerShell, a variable does not need to be predefined to be assigned to use. For example, I want to define a variable to hold all the processes that are in progress on the current computer, so I can do the following:

Copy Code code as follows:

PS c:\users\zhanghong> $proclist =get-process
PS c:\users\zhanghong> $proclist

handles  NPM (k)     PM (k)       WS (k) VM (M)    CPU (s)       Id processname
------- ------   -----     --- -------  ------    -------------
    139       11    16624      16592    52             2660 AUDIODG
     23        5     1984       3232    45      0.03   2108 cmd
     58        8     2380       6896    67      1.12   2088 conhost
     ...

PS c:\users\zhanghong> $proclist. Count
51


There are three commands above, the first command takes all the process information that is in progress on the current computer and puts it in the variable $proclist, the second statement turns out all the current process information, and the third shows the number of elements in the process variable.

About the PowerShell variable, this article on the introduction so much, I hope to help you, thank you!

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.