Introduction to Windows PowerShell



In this article, we will discuss the Windows PowerShell.

What is Windows PowerShell?

Windows PowerShell is a command-line shell designed for Windows targeting system administrators. This shell is similar to that of CSH, BASH on Unix/Linux OS. It is built on using .NET Framework. This shell consists of a console for interactive prompting and a scripting environment to automate the tasks. This shell can accept/return .NET objects. PowerShell uses the concept of command-lets (cmdlets) to interact with it. Command-lets are the basic building blocks that can perform an atomic (single-function) task. We can redirect output of one cmdlet as input to another cmdlet for implementing complex functions. PowerShell consists of more than 100 cmdlets and allows us to create our own cmdlets and share it across. By using PowerShell, we can access our file system, registry etc. 


Unlike other shells like CSH, PowerShell won't process text entered. Instead, it processes .NET objects. PowerShell comes with a handy set of built-in commands to perform basic tasks. All shell commands uses same parser. We can even develop our own cmdlets. First, we will look into built-in cmdlets. A cmdlet is a command that manipulates objects of PowerShell. Most of the cmdlets are easy to understand by their names. Generally, a cmdlet name will have a verb like get or set followed by a noun like "service" or "format" having "-" in between.   For example, get-service will list all the services of our machine. PowerShell is having its own language for implementation, instead of using existing language. We can download PowerShell 1.0 from here.   

PowerShell internally interacts with .NET objects for its working.  For example, when we get all processes of our machine using "get-process" command, it internally gets an object representing .NET Process class. When we view the properties of a process, we are internally getting those using process object's properties. We can get the complete information on a command by using get-help <command-name> -detailed command.

Get Started with Windows PowerShell

Let's start with a simple example, go to your Start menu  >> Windows PowerShell 1.0 >>  Windows PowerShell.

We will type "get-process" to get list of all processes running on our machine. It will list all processes and its properties like process id, process name etc. Now, type "get-process | get-member" command, this will list typename of the .NET object and a list of properties and methods of Process class. In above command, we are redirecting the output of "get-service" as input to "get-member" using pipeline operator (|). By using pipeline operator, we can even redirect the output to a printer or a file. We can use man (UNIX style) command to get help on a particular command. Internally, get-help or man command calls single function to perform the task. This is achieved using aliases of a command. We can list all the cmdlets available using "get-command". Cmdlets can accept parameters identified by a hyphen (-) preceding the parameter name. We no need to type complete parameter name, but you only need to enter enough characters to distinguish it from other parameter names. For example, "get-process" command has a parameter "name", but we can just type "get-process -na winword" to distinguish from other parameter names. 
            
We can format the output of a command using format cmdlets. Following cmdlets are used to format the output:

  • Format-List
  • Format-Custom
  • Format-Table
  • Format-Wide

For example, when we type "get-process | format-list", it will list the output in a clean format in List format. Few cmdlets might be having lengthy names to type, in order to save time we can define aliases for it. We can get list of all aliases using "get-alias" command. We can get a aliases for a particular command using below syntax:

"get-alias | where-object {$_.definition -eq "<cmdlet-name>"}".

Example:

get-alias | where-object {$_.definition -eq "get-process"}

We can create an alias for a command by using below syntax: 

  "set-alias <alias-name> <command-name>".

Example:

set-alias testalias get-process

We can delete an existing alias using:

"remove-item alias:<alias-name>".

Example:

remove-item alias:testalias

We can get the contents of a environment variable using "$env:<variable-name>" command. We can even update an environment variable using

"$env:<variable-name += ";new-value">".


Example:

$env:temp +=";c:\temp"

We can move into windows registry using "cd hklm:" command. Similarly, we can move into certificates using "cd cert:" command.

I am ending up the things here.

Summary

In next article, we will discuss about scripting capabilities of PowerShell. I hope this article will be helpful for all.


Similar Articles