The Secrets of Windows Vista’s BCDEDIT
affled by Windows Vista’s BCDEDIT command-line tool for customizing multiboot startups? Who isn’t? Here’s an easy guide to its basics.
If you’re only looking to customize the basics of your Windows Vista multiboot system, there’s no need to download and install VistaBootPRO [Hack #12]. You can, instead, directly hack the BCD store using BCDEDIT.
Before you get [...]
Convert Numbers Between Bases
1. Problem
You want to convert a number to a different base.
2. Solution
The PowerShell scripting language allows you to enter both decimal and hexadecimal numbers directly. It does not natively support other number bases, but its support for interaction with the .NET Framework enables conversion both to and from binary, octal, decimal, and hexadecimal.
To convert a [...]
Simplify Math with Administrative Constants
1. Problem
You want to work with common administrative numbers (that is, kilobytes, megabytes, and gigabytes) without having to remember or calculate those numbers.
2. Solution
Use PowerShell’s administrative constants (KB, MB, and GB) to help work with these common numbers.
Calculate the download time (in seconds) of a 10.18 megabyte file over a connection that gets 215 kilobytes [...]
Work with Numbers As Binary
1. Problem
You want to work with the individual bits of a number, or work with a number built by combining a series of flags.
2. Solution
To directly enter a hexadecimal number, use the 0x prefix:
       PS >$hexNumber = 0×1234
       PS >$hexNumber
       4660
To convert a number to its binary representation, supply a base of 2 to the [...]
Measure Statistical Properties of a List
1. Problem
You want to measure the numeric (minimum, maximum, sum, average) or textual (characters, words, lines) features of a list of objects.
2. Solution
Use the Measure-Object cmdlet to measure these statistical properties of a list.
To measure the numeric features of a stream of objects, pipe those objects to the Measure-Object cmdlet:
       PS >1..10 | Measure-Object–Average -Sum
       [...]
Perform Complex Arithmetic
1. Problem
You want to use PowerShell to calculate more complex or advanced mathematical results.
2. Solution
PowerShell supports more advanced mathematical tasks primarily through its support for the System.Math class in the .NET Framework.
To find the absolute value of a number, use the [Math]::Abs() method:
       PS >[Math]::Abs(-10.6)
       10.6
To find the power (such as the square or the [...]
Perform Simple Arithmetic
1. Problem
You want to use PowerShell to calculate simple mathematical results.
2. Solution
Use PowerShell’s arithmetic operators:
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
+=, -=, *=, /=, and %=
Assignment variations of the above
( )
Precedence/Order of operations
For a detailed description of these mathematical operators, see “Simple Operators” in Appendix A.
3. Discussion
One difficulty in many programming languages comes from the way that they handle data in [...]
Format a Date for Output
1. Problem
You want to control the way that PowerShell displays or formats a date.
2. Solution
To control the format of a date, use one of the following options:
The Get-Date cmdlet’s –Format parameter:
PS >Get-Date -Date “05/09/1998 1:23 PM” -Format “dd-MM-yyyy @ hh:mm:ss”
09-05-1998 @ 01:23:00
PowerShell’s string formatting (–f) operator:
PS >$date = [DateTime] “05/09/1998 1:23 PM”
PS >”{0:dd-MM-yyyy @ hh:mm:ss}” [...]
Convert a String to Upper/Lowercase
1. Problem
You want to convert a string to uppercase or lowercase.
2. Solution
Use the ToUpper() and ToLower() methods of the string to convert it to uppercase and lowercase, respectively.
To convert a string to uppercase, use the ToUpper() method:
       PS >”Hello World”.ToUpper()
       HELLO WORLD
To convert a string to lowercase, use the ToLower() method:
       PS >”Hello World”.ToLower()
       [...]
Search a String for Text or a Pattern
1. Problem
You want to determine if a string contains another string, or want to find the position of a string within another string.
2. Solution
PowerShell provides several options to help you search a string for text.
Use the –like operator to determine whether a string matches a given DOS-like wildcard:
       PS >”Hello World” -like “*llo W*”
       True
[...]

