Power Shell Common syntaxes

There are two common ways in which you can use PowerShell. First, you can execute commands interactively using the PowerShell console window. Second, you can write scripts to automate administration tasks.

Accessing PowerShell Console window

Start > All Programs > Accessories > Windows PowerShell > Windows PowerShell

Common Help

  • Use the “exit” keyword to exit the shell
  • Ctrl-C will interrupt the current task returning you to the prompt.
  • A command can be spread over multiple lines and the interpreter will prompt for additional input. The line continuation character is the back-quote ‘`’ (also called the back-tick).
  • Ctrl-C will interrupt the current task returning you to the prompt.
  • To get help about a command you can do “help command”. The help command by itself will give you a list of topics.
  • The help command supports wildcards so “help get-*” will return all of the commands that start with “get-”.
  • You can also get basic help on a command by doing “commandName -?” like “dir –?”

Variables in Power Shell

PS > $x= 5                
PS > echo $x
5

PS >

Arrays
PS > $x= Subhasis,Daniel, Mac
PS > echo $x[1]
Daniel

PS >

HashTable

PS > $h= @{a=1;b=2;c=3}
PS > echo $h
Name             Value

a                       1
b                       2
c                       3
Flow-control Statements:

These Statements can be used to iterate the user collections


if Statement:
if ($b –eq 15) { $a=13} else {$a=15}

The condition part of an if statement may also be a pipeline.


if (dir | where {$_.length –gt 10kb})

{
“There were files longer than 10kb”
}

while Loop:

$b=1; while ($b –lt 10) { echo $b }

for Loop:

for ($i=0; $i –lt 10; $i++)

{
echo $i
}

foreach Loop:

foreach ($i in 1..10)

{
$echo $i

}

Blogger Labels: PowerShell,Power Shell Common Syntaxes,PowerShell,PowerShell Control Statements,Power Shell and Sharepoint 2010,Administration using Power Shell and Sharepoint 2010,Start,Help,Ctrl,task,interpreter,continuation,Variables in PowerShell,Subhasis,HashTable in PowerShell,If Statement in PowerShell

0 comments: