Read-Host cmdlet in Powershell

How to Start Service Using Powershell Commands

How to print table in powershell

To print a table for number 7-

$TB=7
for ($i=1; $i -le 10; $i++)
{write-host $i “X” $TB “=” ($TB*$i)}

But if you want to print the table for the number you want to enter, you can do it with the help of Read-Host cmdlet

Save the below script in a notepad name “table.ps1″*

$TB=read-host (“Your No?”)
for ($i=1; $i -le 10; $i++)
{write-host $i “X” $TB “=” ($Ti*$tb)}

then run the file as-
.\table.ps1

Output of the script will be –
1 X 6 = 6
2 X 6 = 66
3 X 6 = 666
4 X 6 = 6666
5 X 6 = 66666
6 X 6 = 666666
7 X 6 = 6666666
8 X 6 = 66666666
9 X 6 = 666666666
10 X 6 = 6666666666
                                          Oooops!!!!  it is not my desired output 😦

So what we have the solution as a very interesting feature of powershell for you

if you will change the order of $TB*$i in write-host cmdlet as $i*$TB your output will be your table 🙂

Your No?: 4
1 X 4 = 4
2 X 4 = 8
3 X 4 = 12
4 X 4 = 16
5 X 4 = 20
6 X 4 = 24
7 X 4 = 28
8 X 4 = 32
9 X 4 = 36
10 X 4 = 40

but the change of order of veriables in the write-host cmdlet has no affact on the output in the simple example.

*ps1 is extension for powershell execution file

INTERVIEW PREPARATION IN 30 MINs – Read Following wires for a quick interview preparation.

  1. SQL Server DBA “Interview Questions And Answers”
  2. SQL Azure Interview Questions and Answers Part – 1
  3. SQL Azure Interview Questions and Answers Part – 2
  4. Powershell Interview Questions and Answers

Regards
Online SQL/Powershell Classes @ http://tuitionaffordable.webstarts.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s