Home | Windows | Network | Interview Questions | Database | Virtualization | Knowledge Base | Contact Us

Quick Links

DNS FAQ's

DHCP FAQ's

Active Directory FAQ's

AD History

Configuring New Domain

Deleted Object Recovery in AD

Global Catalog Server

NetDom Command

Replmon Command

NTDS Utility Guide

FSMO Guide

FSMO Failure

Network Interview Questions

SQL Interview Questions

Windows 2003 KB

Windows 2008 KB

Network KB

Knowledge Base Home

Active Directory Trust

Group Policy Guide

IIS 6.0

RAID Levels

RPC Guide

Domain & Forest Functional Levels

SQL Failover Cluster

PowerShell Tutorial – CmdLets

In common with other scripting languages, in PowerShell   has basic language elements like Variables, Arrays, Functions, Objects, Loops, IF statements, Switch statements, etc. 

PowerShell CmdLets (Commands)

In PowerShell  commands are known as CmdLets (pronounced Command Lets). Cmdlets follow the naming convention of Verb-Noun combination:

E.g: Get-Help, Get-Service, Get-Process

PowerShell cmdlets are not case sensitive and are the smallest unit of functionality in PS.  You can use either the PowerShell Integrated Script Environment shown below (just typepowershell_ise into the search box in the Windows start menu to launch this) or the command line tool to execute Cmdlets.

In the PowerShell ISE. you enter the command at the prompt and the results of the cmdlet will be displayed in the middle panel of the tool. You can use the top panel in the tool to write scripts and also to execute single Cmdlets. The F5 key executes the entire script while F8 key executes only highlighted Cmdlets (Run Selection).

Cmdlets are simple to type, you can use Tab key to auto complete the Cmdlet. E.g: TypeGet-Pro then press the Tab key.

Cmdlet parameters

Cmdlet accept  which are denoted by using the “-“symbol:

For example, the “-Name” is the parameter instructs PowerShell to display only “winrm” service information. Parameters are  also  auto completed  using  the Tab key, so that you don’t have to remember the entire parameter name.

Piping

Piping or pipelining is a method of combining two or more PowerShell Cmdlets to do a single task. PowerShell is a fully objected oriented scripting language as a result Cmdlet   returns an object as result. To combine Powershell Cmdlets you can use the symbol, “|”.

The Get-Service cmdlet returns all services (whatever the state) on the local machine. It returns as an object. The “|” or piping passes that object in to next cmdlet (where-object) which essentially does the filtering.  The braces “{ }” represents the body of the where-object cmdlet, whichs specifies a condition. “$_.Status” is the current object (“$_.”), property (“Status”) and “-eq” is the logical condition (ie “=”).
Operators   in PowerShell user characters and not as symbols as  in other languages.

The Where-object cmdlet iterates through all the objects returned from the Get-Servicecmdlet and filters out only the objects which are have the status of “Running”.

Executing the Get-Command cmdlet will provide a full listing of all the cmdlets available in PowerShell (as well as all the Aliases, Functions etc):

Executing Get-Help for a cmdlet will provide details on how to use the cmdlet, for example say if you wanted to see the usage for the Add-Computer cmdlet.

The Get-Help cmdlet can even provide examples of cmdlets working:

Aliases

Each PowerShell cmdlet associates with an alias. You can use the alias instead of cmdlet full name. Execute Get-Alias for a full listing of all the Aliases in PowerShell (the Definition in the listing is the original Cmdlet name).

If you think cmdlet full name is a bit difficult, you can use short names (ie aliases) but note that this reduces the readability of the script. I prefer to use full names in scripts and aliases for ad-hoc queries.

Scripts

PowerShell scripts are text files with the extension of “.ps1”. You can write PowerShell scripts by using cmdlets and save themfor future use. Commonly used tasks are good candidates for scripts.  By default PowerShell prevents the execution of any script, due to the execution policy of the PowerShell. You can see the current execution policy in PowerShell  using the  Get-ExecutionPolicy cmdlet.

There are four levels of execution policies.

1. Restricted – Default setting of PowerShell. No scripts can run

2. AllSigned- Scripts should have the trusted digital signature

3. RemoteSigned

4. Unrestricted- Any script can run.

You can change the execution policy using the Set-ExecutionPolicy cmdlet.  PowerShell needs to be “Run as Administrator” in order to set execution policy.

<<Previous Page

Windows Server 2008 KB Articles            Windows Server 2003 KB Articles

HTML Comment Box is loading comments...

Home | Windows | Network | Post Ur Issues | Database| Knowledge Base | Contact Us

Designed by TechieBird