PowerShell variables and custom formats

Define the variable:
$a = [int]3
$b = [int]5

Calculated between variables:

1 PS C:\Users\he.shaobo> $a  + $b 
2 8

New variable:

New-Variable -Name abc -Value 123 -Description IntVar -Force

  

Joint assignment between multiple variables:

$a=$b=$c=123  

 

The assignment between variables exchange:

PS E:\host> $value1 = 1;

PS E:\host> $value2 = 2;

PS E:\host> $temp = $value2;$value2 = $value1;$value1 = $temp

PS E:\host> $value1
2

PS E:\host> $value2
1

  

View the current PowerShell Session variable use, including system variables and custom variables:

PS E:\host> ls variable:

Name                           Value                                                                                                                                                  
----                           -----                                                                                                                                                  
$                              4                                                                                                                                                      
?                              True                                                                                                                                                   
^                              Name                                                                                                                                                   
a                              123                                                                                                                                                    
abc                            123                                                                                                                                                    
args                           {}                                                                                                                                                     
b                              123                                                                                                                                                    
c                              123                                                                                                                                                    
ConfirmPreference              High                                                                                                                                                   
ConsoleFileName                                                                                                                                                                       
DebugPreference                SilentlyContinue                                                                                                                                       
Error                          {System.Management.Automation.ParseException: At line:2 char:37...                                                                                     
ErrorActionPreference          Continue                                                                                                                                               
ErrorView                      NormalView                  

 

Verify the existence of variables, test variables:

Test-Path variable:value1

 

Delete variables:

Variable: value1

  

Write-protected variable

You can use the New-Variable option option when creating variables, plus the read-only attribute to the variable, variable value reassignments so do not give up.

PS E:\host> New-Variable num -Value 100 -Force -Option readonly

PS E:\host> $num
100

PS E:\host> $num = $num +1
Cannot overwrite variable num because it is read-only or constant.
At line:1 char:1
+ $num = $num +1
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (num:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

  

Create macro variables, such variables are usually in the entire script environment usually will not change, or that affect the overall situation, and generally will not change.

PS E:\host> new-variable project_ver -Value "1.1.12" -Option constant


PS E:\host> $project_ver
1.1.12

PS E:\host> $project_ver = 1
Cannot overwrite variable project_ver because it is read-only or constant.
At line:1 char:1
+ $project_ver = 1
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (project_ver:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable
 

  

 

Guess you like

Origin www.cnblogs.com/heshaoboHappyAzure/p/11345986.html