PowerShell data types

Overview

In the System Namespace, there are structures which allow us to declare variables to be of a certain data type. The list below shows some commonly used ones.

Datatype Comments
Boolean Represents a Boolean value.
Byte Represents an 8-bit unsigned integer.
Char Represents a character as a UTF-16 code unit.
Decimal Represents a decimal number.
Double Represents a double-precision floating-point number.
Int16 Represents a 16-bit signed integer.
Int32 Represents a 32-bit signed integer.
Int64 Represents a 64-bit signed integer.
SByte Represents an 8-bit signed integer.
Single Represents a single-precision floating-point number.
UInt16 Represents a 16-bit unsigned integer.
UInt32 Represents a 32-bit unsigned integer.
UInt64 Represents a 64-bit unsigned integer.
String Represents text as a series of Unicode characters.

This means we can explicitly declare variables to be of a particular data type. For example:

[Decimal]$fred

declares variable fred to be a Decimal.

equally, fred could be an Int16:

[Int16]$fred

The numeric data types have static properties which allow us to assign the maximum or minimum value allowed to the variable.

If we execute the command:

$fred | Get-Member -Static -MemberType property;

this shows us that we have a MinValue and a MaxValue:

Name     MemberType Definition
----     ---------- ----------
MaxValue Property   static decimal MaxValue {get;}
MinusOne Property   static decimal MinusOne {get;}
MinValue Property   static decimal MinValue {get;}
One      Property   static decimal One {get;}
Zero     Property   static decimal Zero {get;}

To make use of the minimum and maximum values, execute the command:

[Decimal]$fred=[System.Decimal]::MinValue
or
[Decimal]$fred=[System.Decimal]::MaxValue

If we’re using other data types, then replace Decimal for the data type in question.

Reference

System Namespace. The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.

Advertisements
This entry was posted in powershell and tagged , . Bookmark the permalink.

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