Reading And Writing To The Console

Writing Text

Writing to the pipeline

"Hello World"

Writing to the command line

write-host "Hello World"

Writing to the pipeline

"Hello world" | set-content -Path C:\p\100\out.txt

Writing to the command line

write-host "Hello world" | set-content -Path C:\p\100\out.txt

Variables

Creating a variable with text

$myname = "Alice"
write-host "Hello, $myname"

Creating a multiline variable with text

$emailcontent = @"
Hi all,

Just for your information, you can get a free powershell course online

Kind regards,
Timothy
"@
write-host $emailcontent

Interpretation vs no interpretation of variables vs escaping

$myname = "Alice"
write-host "Hello, $myname"
write-host 'Hello, $myname'
write-host "Hello, `$myname"

Creating a variable with a number

$mypi = 3.14159265359
$radius = 15
$surface = $radius*$radius*$mypi
write-host "The surface area of a circle with radius $radius is $surface"

Basic Math

The real basics

$a = 7
$b = 5
$a + $b
$a - $b
$a * $b
$a / $b

Math with Int and Modulo %

$a = 7
$b = 5

[int]$c = $a / $b
$c

$a % $b

Incrementing

$a = 1
$a++ 
write-host "After incrementing $a"
$a--
write-host "After decrementing $a"

Reading Input From The Command Line

Reading a text variable

$myname = read-host "What is your name?"
write-host "Hello, $myname"

Reading a number (does not work)

$height = read-host "What is the height"
$surface = $height*$height
write-host "The $surface of a square with height $height is $surface"

Reading a number

[int]$height = read-host "What is the height"
$surface = $height*$height
write-host "The $surface of a square with height $height is $surface"

Reading a float

[float]$height = read-host "What is the height"
$surface = $height*$height
write-host "The $surface of a square with height $height is $surface"

References: https://technet.microsoft.com/en-us/library/ff642464.aspx

results matching ""

    No results matching ""