Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

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...

  1. 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.

  2. 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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image