Search notes:

PowerShell: Loosly and strongly typed variables

Each object and by implication each variable has a type. In PowerShell, a variable is by default loosly typed, but can be explicitly defined to be strongly typed.
A strongly typed variable always refers to objects of the same type: the type with which the variable was defined. In contrast, the types of the objects a loosly typed variable refers to might change during the existence of the variable.
A loosly typed variable springs into existence when a value is assigned to an undefined variable:
$aVariable = 99
A strongle typed variable is created by using the [type] $var = … construct when a value is assigned to a variable for the first time:
[int] anInteger = 42
When a value of a different type is assigned to a strongly typed variable, PowerShell tries to cast the value to the type of the strongly typed variable. This is demonstrated by the following script:
[int] $anInteger = 42
      $aVariable = 99

$anInteger.GetType().FullName # System.Int32
$aVariable.GetType().FullName # System.Int32

$anInteger = "17"
$aVariable = "32"

$anInteger.GetType().FullName # System.Int32
$aVariable.GetType().FullName # System.String
Github repository about-PowerShell, path: /language/variable/typed-untyped.ps1

Index