Calculate with the console (Simple)
In the previous recipe, we covered techniques to update help and utilize it in a fair manner. This is a short recipe on how we can utilize the Windows PowerShell console as a calculator as well as an editor.
Getting ready
Most of the functionalities are inherited from Version 2, but there are some enhancements in terms of methods and properties.
How to do it...
Let's start playing with the Windows PowerShell console:
PS C :\> "Windows PowerShell" "Windows PowerShell"
If you type any string in quotes into the PowerShell console, it throws a string object as output to the console. It displays the same string that you have quoted.
Now, try converting your console into a calculator:
PS C :\> 2+3 5
The console itself can perform basic calculations, such as addition, subtraction, multiplication, and division.
PS C :\> 8%7 1
It can also perform the modulo operations listed earlier.
How it works…
Let's go one step forward:
PS C :\> "Windows PowerShell" | Get-Member
The previous command displays all the methods and properties available with the string object we have piped earlier.
If you compare the execution of the same command statement between both the Windows PowerShell Versions (2.0 and 3.0), you will get additional methods and properties listed as follows:
TypeName: System.String Name MemberType Definition ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider) ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider) ToChar Method char IConvertible.ToChar(System.IFormatProvider provider) ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider) ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider) ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider) ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider) ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider) ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider) ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider) ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider) ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider) ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider) ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider) ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
Now, retrieve methods and properties for Integer objects:
PS C :\> 2+3 | Get-Member
It displays all the methods and properties available with the integer object we have piped earlier.
If you compare the execution of the same command statement between both the Windows PowerShell Versions (2.0 and 3.0), you will get additional methods and properties listed as follows:
TypeName: System.Int32 Name MemberType Definition ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider) ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider) ToChar Method char IConvertible.ToChar(System.IFormatProvider provider) ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider) ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider) ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider) ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider) ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider) ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider) ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider) ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider) ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider) ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider) ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider) ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
There's more…
The same mechanism is applicable to all the other objects, such as double and so on. For example, take a real-time scenario:
As an administrator, you are dealing with server memory configurations in units such as MB, GB, TB, and so on.
Let's utilize the Windows PowerShell console for such calculations:
PS C :\> 1024MB/1GB 1 PS C :\> 1000MB/1GB 0.9765625
Say you have 2 TB of external storage and you need to create a data drive of 4 GB for each user. How many users can you accommodate in this requirement?
PS C :\> 2TB/4GB 512
Isn't it tricky?
In such scenarios, we can easily utilize the built-in calculation functionality of the Windows PowerShell console.