A variable can automatically store any type information that PowerShell can recognize by $variable's GetType (). Name to view and verify the data type that PowerShell assigns to the variable.
Ps>. GetType (). Name
Int32
ps> (9999999999999999). GetType (). Name
Int64
ps> (3.14). GetType (). Name
Double
ps> (3.14d). GetType (). Name
Decimal
ps> ("WWW. Mossfly.com "). GetType (). Name
String
ps> (get-date). GetType (). Name
Datetimepowershell
Assigns an optimal data type to the data; If an integer exceeds the upper bound of a 32-bit integer ([int32]::maxvalue), it assigns a data type of 64-bit integers, and if a decimal is encountered, a double is assigned; PowerShell is assigned a string type, and if it is a date or time, it is stored as a DateTime object.
This type of adaptation is also known as a "weak type", which, while easy to use, can be limited or even dangerous. If PowerShell chooses a wrong type to pay the variable, it may cause some strange phenomena. For example, there is a variable to store the number of files to be copied, but when assigned a string, PowerShell not to do too much judgment, it will update the type of the variable, and store new data. Therefore, the general professional programmer or script developers prefer to use "strong type", even if the type of the assignment is not compatible with the error, they are willing to accept.
Another reason to like using strong types is that each data type has its own function. For example, DateTime, and XML, although both types can be expressed in plain text, but using strongly typed [DateTime] and [XML] are more convenient for data manipulation, these two types of methods are very rich.
Specify a type definition variable
When you define a variable, you can include the data type in the brackets before the variable. For example, a variable of type byte is defined because the domain of byte is [0,255] and an error message is displayed if an attempt is made to assign the variable to a value that is not in the definition field.
ps> [byte] $b =101
ps> $b
ps> $b =255 ps>
$b
255 ps>
$b. GetType ()
IsPublic isserial Name basetype
-------------------- --------
true Byte System.ValueType
ps> $b =256
cannot convert value "256" to type "System.Byte". Error: "Value is either too large or too small for a unsigned byte.
"
At line:1 char:3
+ $b <<<< =256
+ categoryinfo : metadataerror: (:) [], argumenttransformationmetadataexception
+ fullyqualifiederrorid:runtimeexception
Advantages of using fixed types
One important reason for manually defining a type is that each particular data type has its own special commands and special methods. For example, to assign a date string to a variable, PowerShell will not automatically convert the string into a Date object assigned to a variable, because PowerShell is a machine, no one so intelligent. When you specify a datetime type when assigning a value, you will find that almost all methods of datetime types in. Net are supported here.
ps> [DateTime] $date = "2012-12-20 12:45:00"
ps> $date
December 20, 2012 12:45:00 ps>
$date. DayOfWeek
Thursday
ps> $date. DayOfYear
355
ps> $date. AddDays ( -10)
December 10, 2012 12:45:00powershell
It's also convenient to work with XML documents,
For example, there are the following logotest.xml
<LOGOTEST>
<EXTENSIONS>
<E>.exe</E>
<E>.dll</E>
</ extensions>
<FILES>
<F></F>
</FILES>
<DIRS></DIRS>
</LOGOTEST>
querying for. exe and. DLL nodes
ps> [XML] $xml = (get-content. Logotestconfig.xml)
ps> $xml. LOGOTEST.EXTENSIONS.E.
exe
Supported by default. NET types are as follows.
[array],[bool],[byte],[char],[datetime],[decimal],[double],[guid],[hashtable],[int16],[int32],[int],[int64],[ Long],[nullable],[psobject],[regex],[sbyte]. [Scriptblock],[single],[float],[string],[switch],[timespan],[type],[uint16],[uint32],[uint64],[XML]