Windows Powershell Define Variables _powershell

Source: Internet
Author: User

Variables can store data temporarily, so you can save the data in a variable for further action.

#定义变量
$a =10
$b =4
#计算变量
$result = $a * $b
= "Save Text"

$msg #输出变量

40
Save Text

PowerShell do not need to be displayed to declare, you can automatically create a variable, just remember that the variable prefix is $.

After you have created the variable, you can output the variable by the variable name, or you can place the variable name in the string. But there's an exception. Strings in single quotes do not recognize and process variable names.

Select variable Name

In PowerShell, variable names start with the dollar sign "$", and the remaining characters can be numbers, letters, and any underlined characters, and PowerShell variable names are not case-sensitive ($a and $a are the same variable).

Some special characters are used for special purposes in PowerShell, and these characters are generally not recommended as variable names. Of course you want to use, please enclose the entire variable name suffix in curly braces.

PS c:\test> ${"i" Like $}= "Mossfly"
PS c:\test> ${"I" Like $}

Mossfly Assignment and return value

The assignment operator is "=", which can almost assign any data to a variable, or even a cmdlet command
, why, because PowerShell supports objects, objects can be all-encompassing.

 PS c:\test> $item =get-childitem. PS c:\test> $item directory:c:\test Mode lastwritetime Length Name-------------------------- -D----2011/11/23 17:25 abc-a---2011/11/24 18:30 67580 a.html-a---2011/11/24 20:04 26384 a.tx     T-a---2011/11/24 20:26 12060 alias-a---2011/11/24 20:27 12060 alias.ps1-a---2011/11/23 17:25  0 b.txt-a---2011/11/23 17:25 0 c.txt-a---2011/11/23 17:25 0 d.txt-a---2011/11/25 11:20 556 Employee.xml-a---2011/11/24 17:37 7420 name.html-a---2011/11/28 15:30-ping.bat-a---2011/11/2 4 17:44 735892 powershell_cmdlets.html-a---2011/11/28 17:03 test.ps1-a---2011/11/23 17:37 242 Test.txt-a---2011/11/28 16:42 170 test.vbs PS c:\test> $result =3000* (1/12+0.0075) PS c:\test> $result 272 .5 

Assign values to multiple variables at the same time
Assignment operators not only assign values to a variable, they can also assign the same value to multiple variables.

PS c:\test> $a = $b = $c =123
PS c:\test> $a
123
PS c:\test> $b
123
PS c:\test> $c
123

Value of the Exchange variable

To swap the values of two variables, the traditional program language requires at least three steps and a temporary intermediate variable is defined.

$Value 1 = ten
$Value 2 =
$Temp = $Value 1
$Value 1 = $Value 2
$Value 2 = $Temp

In PowerShell, swapping the values of two variables makes the function very simple.

PS c:\test> $value 1=10 PS
c:\test> $value 2=20
PS c:\test> $value 1, $value 2= $value 2, $value 1
PS c:\test> $value 1
PS c:\test> $value 2
10

View the variables that are in use
PowerShell A record of information about a variable in a driver named variable:. If you want to see all the defined variables, you can traverse variable directly:

PS c:\test> ls variable:

Name              Value
----              -----
"I" like $           mossfly
$               cls
?               True
^               CLS
_
1               1
a               123
args              {}
b               123
C               123
confirmpreference       High
consolefilename
debugpreference        silentlycontinue
...

Find variables

Because of the virtual drive variable: The existence of a wildcard can be used to find a variable like a file. For example, to query for variable names that begin with value.

PS c:\test> ls variable:value*

Name              value
----              -----
value1
value2             10

Verify that the variable exists

Verify that a variable exists, and you can still use the cmdlet Test-path as you would to verify the file system. Why? Because the variable exists in the variable drive.

PS c:\test> test-path variable:value1
true
PS c:\test> test-path variable:value2
True
PS c:\ test> Test-path variable:valueunkonw
False

Delete a variable

Because the variable is automatically cleared when the PowerShell exits or closes. There is no need to delete it, but you have to delete it, or you can delete it as if you were deleting the file.

PS c:\test> test-path variable:value1
True
ps c:\test> del variable:value1
PS c:\test> test-path Variable:value1
False

Use a dedicated variable command

To manage variables, PowerShell provides five command clear-variable,get-variable,new-variable,remove-variable,set-variable that specialize in managing variables. Because the virtual drive variable: The presence of the Clear,remove,set command can be replaced. But get-variable,new-variable. It's very useful. New-variable can specify some other properties of a variable, such as access rights, when defining a variable. The same get-variable can also get these additional information.

Variable write protection

You can use the New-variable option to add a read-only property to a variable when you create a variable, so you can't reassign the variable.

PS c:\test> new-variable num-value 100-force-option readonly
PS c:\test> $num =101 cannot overwrite Variab
Le num because it is read-only or constant.
At line:1 char:5
+ $num <<<< =101   + categoryinfo     : writeerror: (num:string) [], Sessionstateunauthorizedaccessexception   + fullyqualifiederrorid:variablenotwritable PS C:\test> del Variable : num
remove-item:cannot Remove variable num because it is constant or read-only. If the variable is read-only,
ration again specifying the Force option.
At Line:1 Char:4
+ del <<<< variable:num
  + categoryinfo     : writeerror: (num:string) [ Remove-item], Sessionstateunauthorizedaccessexcepti
  + fullyqualifiederrorid:variablenotremovable, Microsoft.PowerShell.Commands.RemoveItemCommand

However, you can update the variable content by deleting the variable and then recreating the variable.

PS c:\test> del variable:num-force
PS c:\test> $num =101
PS c:\test> $num
101

There is no higher permission variable, there is, that is: option constant, constants once declared, cannot be modified

PS c:\test> new-variable num-value "Strong"-option constant

ps c:\test> $num = "Why? can not delete it."
Cannot overwrite variable num because it is read-only or constant.
At line:1 char:5
+ $num <<<< = "Why? can not delete it."   + categoryinfo     : writeerror: (num:string) [], sessionstateunauthorizedaccessexception   + Fullyqualifiederrorid:variablenotwritable PS c:\test> del variable:num-force
remove-item:cannot Remove Variab Le num because it is constant or read-only. If the variable is read-only,
ration again specifying the Force option.
At Line:1 Char:4
+ del <<<< variable:num-force
  + categoryinfo     : writeerror: (num:string) [ Remove-item], Sessionstateunauthorizedaccessexcepti
  + fullyqualifiederrorid:variablenotremovable, Microsoft.PowerShell.Commands.RemoveItemCommand

Variable description
in new-variable, variable descriptions can be added by-description, but variable descriptions are not displayed by default and can be viewed by format-list.

PS c:\test> new-variable name-value "Me"-description "This are my name"
PS c:\test> ls variable:name | fl *
   
    pspath    : Microsoft.powershell.corevariable::name
psdrive:    Variable
psprovider  : Microsoft.PowerShell.CoreVariable
psiscontainer:false
name     : Name
Description  : This is my Name
Value     : Me
Visibility  : Public
Module    :
modulename  :
Options    : None
Attributes  : {}

   

Related Article

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.