Powershell Day 4

Day 4 Programming in Powershell Part 2

Now we got some basic information about shell programming operators and other stuff. In this blog we will try to discuss more about programming in Powershell.

Here most of the things will be similar to “C” programming language – you might have observed so far 🙂. The core PowerShell language is based on the POSIX 1003.2 grammar for the Korn shell. As Powershell is compatible with .Net Framework We can easliy migrate code between PowerShell and C#.

Powershell commands – Cmdlets is simple readable format as Verb-Noun eg. Get-childitem

There are some commands alias which interns runs the command like “dir” is alias for get-childitem  and Clear/cls from clear-host

PS C:\> get-command clear

 

CommandType     Name                            Definition

———–     —-                            ———-

Alias           clear                           Clear-Host

 

 

PS C:\> get-command cls

 

CommandType     Name                            Definition

———–     —-                            ———-

Alias           cls                             Clear-Host

In day 2 of this blog Comparison of cmdlets with similar commands(From Wikipedia*) shows some of the cmdlets with alias here

To get the complete detail of any command pipe that command with get-command <Command> |fl  (FormatList)

*more formating information is discussed later

PS C:\> Get-Command get-childitem |fl

Backtick (shift + (tilt) )escape sequences for string

`n         Newline

`r          Carriage return

`t          Horizontal tab

`a         Alert

`b         Backspace

`’          Single quote

`”          Double quote

`0         Null

“          A single backtick

The escape sequence can only be good when you use string with double-quote  “.

Data type:

When you create a variable it can be assigned to any data type.

PS C:\> $a=”Hello-World”

PS C:\> $a

Hello-World

PS C:\> $a=123

PS C:\> $a

123

Now if we want to explicitly assign a data type to a variable.

To write a variable to string : assign the variable start with @” and end with “@ , And string withing the block will be printed as it is.

and if we want to type things in tabular format assign the string starts with @{ and end with }

Array: assigning an array can be done using comma ,

PS C:\> [int]$a=1

PS C:\> $a

1

PS C:\> $a=”avc”

This will give an error

Cannot convert value “a” to type “System.Int32”. Error: “Input string was not i
n a correct format.”
At line:1 char:3
+ $a= <<<< “a”

Following are the list of data types supported by Powershell

Type Alias
[int]
[long]
[string]
[char]
[bool]
[byte]
[double]
[decimal]
[float]
[single]
[regex]
[array]
[xml]
[scriptblock]
[switch]
[hashtable]
[psobject]
[type]
Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s