PowerShell does not distingush variable types. Rather PowerShell automatically declares the data type of variable when it is created. Data type is determined by data input.
Let's create variables by inputting data and figure out variable type with built-in PowerShell function, "GetType"
[Variable Data Type]
String
String variable is used when storing a mixture of characters, number and special characters
Declare a variable name with "$" sign then equal sign(=) then data enclosed in double quotes("")
PS C:\Users\jaehui.yoon> $temp = "Hello World"
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Integer
Integer variable is used when storing a number without decimal point
Declare a variable name with "$", equal sign(=) then number
PS C:\Users\jaehui.yoon> $temp = 1
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
Double
Double variable is used when storing a number with decimal point
Declare a variable name with "$", equal sign(=) then number with decimal point
PS C:\Users\jaehui.yoon> $temp = 3.14
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Double System.ValueType
Boolean
Boolean variable is used when storing a state either true or false like a binary value.
Declare a variable name with "$", equal sign(=) then dollor sign($) the "true" or "false"
PS C:\Users\jaehui.yoon> $temp = $true
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Boolean System.ValueType
Array
Array variable is used when storing a series of values. It could be any type of values mentioned above.
Declare a variable name with "$", equal sign(=) then a series of numbers or/and strings with comma(,) as separator
PS C:\Users\jaehui.yoon> $temp = 1,2,3, "hi", "bye"
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Hash table
Hash table is used when storing values with tag such as "name" = "jaehee yoon"
Hash table respresents a collection of key-value pairs. Data needs to be input enclosed in curly braces({})
PS C:\Users\jaehui.yoon> $temp = @{Name="J"; Age=5; City="Busan"}
$temp.GetType()
$temp
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object
Key : Name
Value : J
Name : Name
Key : Age
Value : 5
Name : Age
Key : City
Value : Busan
Name : City
It is feasible to add/delete hash data after the hash table is created with the format below.
PS C:\Users\jaehui.yoon> $temp["GF"]="Honey"
PS C:\Users\jaehui.yoon> $temp
Name Value
---- -----
GF Honey
Name J
Age 5
City Busan
* Null
PowerShell supports creating variable without data. It can be done by explicitly input $null or without inputting data for a variable.
* When a variable created without data, data type cannot be determined so that GetType() function returns an error
PS C:\Users\jaehui.yoon> $temp = $null
$temp.GetType()
null 값 식에서 메서드를 호출할 수 없습니다.
위치 줄:2 문자:1
+ $temp.GetType()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
[Variable Conversion]
There would be cases that a variable needs to be converted from one type to another. Three ways are availble for converting variables in PowerShell.
1. Cast operator
Cast operator[data type] can be placed after equal sign(=) then this can convert date type
PS C:\Users\jaehui.yoon> $temp = "42"
$temp.GetType()
$temp = [int]$temp
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
True True Int32 System.ValueType
2. Parse method
"Parse" method can be used to convert a string to a type
PS C:\Users\jaehui.yoon> $temp = "42"
$temp.GetType()
$temp = [int]::parse($temp)
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
True True Int32 System.ValueType
3. Conversion method
There are number of data converting method which can convert data type
PS C:\Users\jaehui.yoon> $temp = "42"
$temp.GetType()
$temp = [Convert]::ToInt32($temp)
$temp.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
True True Int32 System.ValueType
'ICT' 카테고리의 다른 글
[PowerShell]Checking User EmailBox Storage Status (0) | 2023.03.30 |
---|---|
[PowerShell]5. Controls(If statement) (0) | 2023.03.28 |
[PowerShell]Fetching Rows from CSV File and Execute PowerShell command for setting EmployeeID (0) | 2023.03.27 |
[PowerShell]3. Inputs and Outputs (0) | 2023.03.23 |
[PowerShell] Saving Activie Directory Query Output to CSV (0) | 2023.03.20 |
댓글