Skip to main content

Creating New Objects by Hash Table Conversion

Beginning in PowerShell 3.0, you can create pre-initialized objects by using a hash table. Simply add the properties you want to preinitialize, then convert the hash table to the desired type.

Here is a practical example:


#requires -Version 3

$preInit = @{
  Rate = -10
  Volume = 100
}

Add-Type -AssemblyName System.Speech
$speaker = [System.Speech.Synthesis.SpeechSynthesizer] $preInit
$null = $Speaker.SpeakAsync(Oh boy, that was a New Years party. I guess I need a little break.”)

When you run this code, PowerShell creates a new System.Speech object and preinitializes the values for rate and volume. When you output text to speech using the SpeakAsync() method, the text is spoken very slowly. Rate accepts values between -10 and 10.

Twitter This Tip! ReTweet this Tip!


Posted Jan 08 2016, 06:00 AM by ps1

upggr

I am the one