fbpx Windows | HostingUltraso.com
24/7/365 Support

Windows

Calculations and Math in Windows PowerShell

Math is an important feature in any scripting language. Math support in a language includes addition, subtraction, multiplication, and division of course, but extends further into more advanced mathematical operations. So, it should not surprise you that PowerShell provides a strong suite of mathematical and calculationoriented features.

Since PowerShell provides full access to its scripting language from the command line, though, this keeps a powerful and useful commandline calculator always at your fingertips!

Program: Convert Text Streams to Objects in Windows PowerShell

One of the strongest features of PowerShell is its objectbased pipeline. You don’t waste your energy creating, destroying, and recreating the object representation of your data. In other shells, you lose the fullfidelity representation of data when the pipeline converts it to pure text. You can regain some of it through excessive text parsing, but not all of it.

However, you still often have to interact with lowfidelity input that originates from outside PowerShell. Textbased data files and legacy programs are two examples.

How to Format a Date for Output in Windows PowerShell

Problem

You want to control the way that PowerShell displays or formats a date.

Solution

To control the format of a date, use one of the following options:

• The GetDate cmdlet’s –Format parameter:

PS >GetDate Date "05/09/1998 1:23 PM" Format "ddMMyyyy @ hh:mm:ss" 09051998 @ 01:23:00

• PowerShell’s string formatting (f) operator:

PS >$date = [DateTime] "05/09/1998 1:23 PM" PS >"{0:ddMMyyyy @ hh:mm:ss}" f $date 09051998 @ 01:23:00

• The object’s ToString() method:

Convert a String to Upper/Lowercase in Windows PowerShell

Problem

You want to convert a string to uppercase or lowercase.

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()

hello world

Discussion

Replace Text in a String in Windows PowerShell

Problem

You want to replace a portion of a string with another string.

Solution

PowerShell provides several options to help you replace text in a string with other text.

Use the Replace() method on the string itself to perform simple replacements:

PS >"Hello World".Replace("World", "PowerShell")

Hello PowerShell Use PowerShell’s regular expression –replace operator to perform more advanced regular expression replacements:

PS >"Hello World" replace '(.*) (.*)','$2 $1' World Hello

Discussion

Search a String for Text or a Pattern in Windows PowerShell

Problem

You want to determine if a string contains another string, or want to find the position of a string within another string.

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 DOSlike wildcard:

PS >"Hello World" –like "*llo W*"

True Use the –match operator to determine whether a string matches a given regular expression:

PS >"Hello World" –match '.*l[lz]o W.*$' True

Place Formatted Information in a String in Windows PowerShell

Problem

You want to place formatted information (such as rightaligned text or numbers rounded to a specific number of decimal places) in a string.

Solution

Use PowerShell’s formatting operator to place formatted information inside a string.

PS >$formatString = "{0,8:D4} {1:C}`n" PS >$report = "Quantity Price`n" PS >$report += "`n" PS >$report += $formatString f 50,2.5677 PS >$report += $formatString f 3,9 PS >$report Quantity Price

0050 $2.57 0003 $9.00

Discussion

Prevent a String from Including Dynamic Information in Windows PowerShell

Problem

You want to prevent PowerShell from interpreting special characters or variable names inside a string.

Solution

Use a nonexpanding string to have PowerShell interpret your string exactly as entered. A nonexpanding uses the single quote character around its text.

PS >$myString = 'Useful PowerShell characters include: $, `, " and { }' PS >$myString Useful PowerShell characters include: $, `, " and { }

If you want to include newline characters as well, use a nonexpanding here string, as in Example 52.

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say