Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

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.