Introduction to 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 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 the 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. 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 use the 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 has its own language for implementation, instead of using an 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 the "get-process" command, it internally gets an object representing the .NET Process class. When we view the properties of a process, we are internally getting those using the process object's properties. We can get the complete information on a command by using the 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 a list of all processes running on our machine. It will list all processes and their properties like process id, process name, etc. Now, type the "get-process | get-member" command, this will list type name of the .NET object and a list of properties and methods of the Process class. In the above command, we are redirecting the output of "get-service" as input to "get-member" using the pipeline operator (|). By using a pipeline operator, we can even redirect the output to a printer or a file. We can use the man (UNIX style) command to get help on a particular command. Internally, the get-help or man command calls a single function to perform the task. This is achieved using the 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 do need to type the 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 it from other parameter names.

We can format the output of a command using format cmdlets. The 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 have lengthy names to type, in order to save time we can define aliases for it. We can get list of all aliases using the "get-alias" command. We can get aliases for a particular command using the 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 the 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 an 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 the Windows registry using "cd hklm:" command. Similarly, we can move into certificates using "cd cert:" command.

I am ending up with the things here.

Summary

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


Similar Articles