<#

$a = “12” ; $b = “4”
$a.GetType().Name  #String
$b.GetType().Name  #String
#######################################

$c = $a + $b
$c                                  #124 (results are concatenated)
#Without casting, Powershell determines type from content.
$c.GetType().Name   #Int32

$c -as [string]              #124
[string]$c                     #124
$c.GetType().Name    #Still Int32
#######################################

#Instance casting of the content
$c =  $($a -As [int]) / $($b -As [int])
$c
3

$c.GetType().Name  #Still Type Int32
$b.GetType().Name  #Still Type String
$a.GetType().Name  #Still Type String
#######################################

#Re-casting variables contents
$c = [int]$a / [int]$b
$c
3

$c.GetType().Name   #Still Type Int32
$b.GetType().Name   #Still Type String
$a.GetType().Name   #Still Type String
#######################################

#Casting the variable
PS C:\> [int]$c = $a / $b
PS C:\> $c
3

$c.GetType().Name   #Still Type Int32
$b.GetType().Name   #Still Type String
$a.GetType().Name   #Still Type String
#######################################

tags: Casting, Casting on the fly, Copy & Paste, Powershell, $S, type, type conversion

#>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s