Table of Contents

Value Type Conversion

In Programming all objects and values have a specific data type to them. The data type determines the interaction of the data within the programming framework.

Type values are used all throughout programming and conversely PowerShell as without knowing what the value of an object/defined variable is the manipulation of the data through adding, subtracting, modifying could give an different result then what was required.

Date types are just alias for .NET Framework classes which makes it easier to use. e.g. the [datetime] is an alias for [system.datetime] namespace class.

Note: "Value type" and "Data type" can be used interchangeably. Value type is the common known programming framework of defining data whereas Data type is Microsoft's way to reference the same definition.

To give an example on what common data types are in Powershell that you would use are below.

Common Data Types
Data Type Description
[Array] An collection of indexed multiple values. it can be any number of different data tpyes e.g. $object = "1","Hello","-18"
[Bool] A data type with only 2 Values, TRUE or FALSE. An Boolean object is only set to match 2 conditions $TRUE or $FALSE
[DateTime] A data type that specifies the value of the object/defined value in the common date and time format readable by the programming language
[HashTable] A arrayed collection of key-pair values; or 2 or more values combined into 1 index array.
[int] Any numeric value from -2147483648 to 2147483647 as seen by running command
[PsObject] The data type of a Powershell Object. When defining a custom object with multiple data types an empty object is created [psobject]
[Regex] A regular expression value. A value that defines a pattern that can match another value. See section 1.4 Strings and Arrays
[Float] A decimal separated numeric value from -3.402823E+38 to 3.402823E+38 as seen by running command
[String] Any alphabetical and numeric text as defined within an double quotation mark e.g.
[string]$object="This is a string",/div>
[XmlDocument] The collection of tagged text that makes up the XML markup language and is readable and parsable by the programming language

What is Casting?

Well, all casting really means is taking an Object of one particular type and “turning it into” another Object type. This process is called casting a variable.

By inputting the data type in front of the object, we have defined the objects data type. Powershell and many other languages are intelligent enough to know what the data type is based on the value. E.g. An number "1" is of type Integer. But this is not always a hard rule is dependent on the manipulation of the variable/object in use.

To define a data type we may put the alias or short name of the data type in front of the object or defined value. e.g.

To then convert of change that existing objects data type to another data type the string type. Simply input the desired alias/shortn ame infront of the new object and reference the existing object. e.g.
[string]$object = $object

To determine the existing data type of an object, you may output the object into the get-member command an view the type at the top of the output. e.g.
$object | get-member

Powershell is just a wrapper over .NET. to Recap .NET is the legacy underlying framework of how Microsoft was programmed in old legacy programming language such as Visual Basic programming language.

This means Powershell programming language has been developed keeping in mind the existing .NET framework model that was used to build the Windows operating system.

Type Accelerators

To find the colloquial powershell short name aliases used such as like [int],[wmi], [regex] etc. (Known as Type Accelerators) run the query below. You may notice the few that you have used so far in this course.

Note: The System.Management.Automation.TypeAccelerators .NET Framework class is private. And cannot be found on Microsoft Docs webpage.

$AcceleratorType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")

To have a complete listing of all the existing data types of objects/defined variables are taken from .NET they can be found using the code below.
[AppDomain]::CurrentDomain.GetAssemblies() |Foreach-Object { $_.GetExportedTypes() }

Data Type manipulation commands

The below process shows how one can convert using purely commands to achieve the same goal but the coding will become more verbose and less scalable to other programming languages as it uses powershell commands instead of basic Programming framework of Data Types.

What you choose to achieve the conversion of casting/converting data types is up to you, but learning both provides means of being able to read other programming languages that use a common definition of object data types.

Hex Byte String

$string = "hello"

#Converting to

$hex = $string | Format-Hex

#Converting to

$bytes = $hex.Bytes

#Converting back to

$string = [System.Text.Encoding]::ASCII.GetString($bytes)