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

Windows

Program: Invoke a Script Block with Alternate Culture Settings

Given PowerShell’s diverse user community, scripts that you share will often be run on a system set to a language other than English. To ensure that your script runs properly in other languages, it is helpful to give it a test run in that culture. Example 126 lets you run the script block you provide in a culture of your choosing.

Example 126. UseCulture.ps1

Write Culture-Aware Scripts in PowerShell

Problem

You want to ensure that your script works well on computers from around the world.

Solution

To write cultureaware scripts, keep the following guidelines in mind as you develop your scripts:

  • Create dates, times, and numbers using PowerShell’s language primitives.
  • Compare strings using PowerShell’s builtin operators.
  • Avoid treating user input as a collection of characters.
  • Use Parse() methods to convert user input to dates, times, and numbers.

Discussion

Provide Progress Updates on Long-Running Tasks in PowerShell

Problem

You want to display status information to the user for longrunning tasks.

Solution

To provide status updates, use the WriteProgress cmdlet as shown in Example 125.

Example 125. Using the WriteProgress cmdlet to display status updates

$activity = "A longrunning operation"

$status = "Initializing" ## Initialize the longrunning operation for($counter = 0; $counter lt 100; $counter++) {

Display Messages and Output to the User in PowerShell

Problem

You want to display messages and other information to the user.

Solution

To ensure that the output actually reaches the screen, call the WriteHost (or OutHost) cmdlet:

PS >function GetDirectorySize >> { >> $size = (GetChildItem | MeasureObject Sum Length).Sum >> WriteHost ("Directory size: {0:N0} bytes" f $size) >> } >> PS >GetDirectorySize Directory size: 46,581 bytes PS >$size = GetDirectorySize Directory size: 46,581 bytes

Read a Key of User Input in Windows PowerShell

Problem

You want your script to get a single keypress from the user.

Solution

For most purposes, use the [Console]::ReadKey() method to read a key: PS >$key = [Console]::ReadKey($true)

PS >$key

KeyChar
Key
Modifiers

h
H
Alt

For highly interactive use (for example, when you care about key down and key up), use:

PS >$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") PS >$key

VirtualKeyCode Character ControlKeyState KeyDown 16 ...ssed, NumLockOn True

Read a Line of User Input in Windows PowerShell

Problem

You want to use input from the user in your script.

Solution

To obtain user input, use the ReadHost cmdlet:

PS >$directory = ReadHost "Enter a directory name" Enter a directory name: C:\MyDirectory PS >$directory C:\MyDirectory

Discussion

The ReadHost cmdlet reads a single line of input from the user. If the input contains sensitive data, the cmdlet supports an –AsSecureString parameter to read this input as a SecureString.

Windows PowerShell User Interaction

While most scripts are designed to run automatically, you will frequently find it useful to have your scripts interact with the user.

The best way to get input from your user is through the arguments and parameters to your script or function. This lets your users to run your script without having to be there as it runs!

If your script greatly benefits from (or requires) an interactive experience, PowerShell offers a range of possibilities. This might be simply waiting for a keypress, prompting for input, or displaying a richer choicebased prompt.

Sort a Hashtable by Key or Value in Windows PowerShell

Problem

You have a hashtable of keys and values, and want to get the list of values that result from sorting the keys in order.

Solution

To sort a hashtable, use the GetEnumerator() method on the hashtable to gain access to its individual elements. Then use the SortObject cmdlet to sort by Name or Value.

foreach($item in $myHashtable.GetEnumerator() | Sort Name)

{

$item.Value

}

Discussion

Create a Hashtable or Associative Array

Problem

You have a collection of items that you want to access through a label that you provide.

Solution

To define a mapping between labels and items, use a hashtable (associative array):

PS >$myHashtable = @{} PS > PS >$myHashtable = @{ Key1 = "Value1"; "Key 2" = 1,2,3 } PS >$myHashtable["New Item"] = 5

PS >

PS >$myHashTable

Name
Value

Key 2
{1, 2, 3}

New Item
5

Key1
Value1

Discussion

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