Book Image

Mastering Windows PowerShell Scripting - Third Edition

By : Chris Dent
Book Image

Mastering Windows PowerShell Scripting - Third Edition

By: Chris Dent

Overview of this book

PowerShell scripts offer a handy way to automate various chores, however working effectively with these scripts can be a difficult task. This comprehensive guide starts with the fundamentals before moving on to advanced-level topics to help you become a PowerShell Core 6.0 expert. The first module, PowerShell Core 6.0 Fundamentals, begins with the new features of PowerShell Core 6.0, installing it on Linux, and working with parameters, objects and .NET classes from within PowerShell Core 6.0. As you make your way through the chapters, you'll see how to efficiently manage large amounts of data and interact with other services using PowerShell Core 6.0. You'll be able to make the most of PowerShell Core 6.0's powerful automation feature, where you will have different methods available to parse data and manipulate regular expressions and Windows Management Instrumentation (WMI). After having explored automation, you will enter the extending PowerShell Core 6.0 module, covering asynchronous processing and desired state configuration. In the last module, you will learn to extend PowerShell Core 6.0 using advanced scripts and filters, and also debug issues along with working on error handling techniques. By the end of this book, you will be an expert in scripting with PowerShell Core 6.0.
Table of Contents (27 chapters)
Free Chapter
1
Section 1: Exploring PowerShell Fundamentals
6
Section 2: Working with Data
16
Section 3: Automating with PowerShell
19
Section 4: Extending PowerShell

Converting strings into numeric values

In most cases, strings may be cast back to numeric values. Consider the following example:

[Int]"2"             # String to Int32 
[Decimal]"3.141"     # String to Decimal 
[UInt32]10           # Int32 to UInt32 
[SByte]-5            # Int32 to SByte 

For advanced conversions, the System.Convert class may be used. The Convert class includes static methods that can take a string and convert it into a number using a specified base.

A binary (base 2) value is converted as follows:

[Convert]::ToInt32('01000111110101', 2)  # Returns 4597 

A hexadecimal (base 16) value can be converted like so:

[Convert]::ToInt32('FF9241', 16)  # Returns 16749121 

The bases that Convert supports are 2 (binary), 8 (octal), 10 (denary), and 16 (hexadecimal).