Basics of PowerShell

PowerShell Overview
It’s a Command Line Interface (CLI) uses different approach to expose information rather than GUI. CLI’s don’t have any pattern that helps us to learn the interface. So we have to remember all the commands before enter to CLI. Also terse names are used over clear once.
Windows PowerShell is based on objects. Whatever we type in text is stored as objects and output of one can be easily passed as input to another command. Windows PowerShell is based on the .NET Framework. Syntax features and keywords of PowerShell are very similar to C# programming language. So knowing C# makes easier to work with PowerShell. Also learning PowerShell makes it much easier to learn C#.
Commands executed in PowerShell are known as cmdlets (pronounced command-lets)

How to use commands
Lets see how to reach a command without knowing its complete name.
First find all the commands nearly close to known keyword. E.g. to find a list of cmdlets that view and change Windows service.

 get-command *-service

Now from list we can choose one command which we feel more relevant as per our requirement. Then we get complete details about that command using get-help command.

 get-help get-service


get-service -?

We have figure out that get-service is the command that we needed. Let’s run this cmdlet to displays information about the members of the object output by the Get-Service cmdlet.

get-service | get-member

Cmdlets Naming
Cmdlets Use Verb-Noun Names to Reduce Command Memorization.
To list all commands that include a particular verb with the -Verb parameter for Get-Command. For example, to see all cmdlets that use the verb Get, type:

Get-Command -Verb Get

The -Noun parameter is even more useful because it allows you to see a family of commands that affect the same type of object. For example, if you want to see which commands are available for managing services, type following command:

Get-Command -Noun Service

Parameters with Cmdlets
Parameter names always have a ‘-‘ prepended to them when you use them, to allow Windows PowerShell to clearly identify them as parameters. In the Get-Command -Name Clear-Host example, the parameter’s name is Name, but it is entered as –Name.

The Help Parameter (?)
When you specify the -? parameter to any cmdlet, the cmdlet is not executed. Instead, Windows PowerShell displays help for the cmdlet.

Common Parameters
Windows PowerShell has several parameters known as common parameters. Because these parameters are controlled by the Windows PowerShell engine, whenever they are implemented by a cmdlet, they will always behave the same way. The common parameters are WhatIf, Confirm, Verbose, Debug, Warn, ErrorAction, ErrorVariable, OutVariable, and OutBuffer.
How to get syntax of a command
To get the syntax of the Get-Help cmdlet, use the following command:

Get-Command Get-Help –Syntax

Displaying Available Command Types
To get command aliases, which are the assigned nicknames of commands, type:

Get-Command -CommandType Alias

To get the functions in the current session, type:

Get-Command -CommandType Function

To display scripts in Windows PowerShell’s search path, type:

Get-Command -CommandType Script

Using Tab Expansion

Tab expansion is controlled by the internal function TabExpansion. Windows PowerShell allows you to fill in file names and cmdlet names by pressing the Tab key. To fill in a filename or path from the available choices automatically, type part of the name and press the Tab key. Windows PowerShell will automatically expand the name to the first match that it finds. Pressing the Tab key repeatedly will cycle through all of the available choices.

One limitation of the tab expansion process is that tabs are always interpreted as attempts to complete a word. If you copy and paste command examples into a Windows PowerShell console, make sure that the sample does not contain tabs; if it does, the results will be unpredictable and will almost certainly not be what you intended.

Enable PowerShell ISE for Windows Server 2008 R2

Windows PowerShell ISE (Integrated Scripting Environment) is a host application for Windows PowerShell.In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphical user interface. Its features include multiline editing, tab completion, syntax coloring, selective execution, context-sensitive Help, and support for right-to-left languages.

By default this feature is shipped with PowerShell 2. If you are not able to find shortcut for Windows PowerShell ISE, it means you have to add this feature explicitly. Following steps will guide you how to add this feature:

Step 1 – Open PowerShell and Import Module Server Manager

            Exec command: Import-Module ServerManager

Step 2 – Now search for PowerShell ISE featurein Windows Features

            Exec command: Get-WindowsFeature -Name *PowerShell*

Step 3 – Verify PowerShell-ISE feature must present in above command result list. Now lets add this feature.

            Exec command: Add-WindowsFeature PowerShell-ISE

After executing above commands successfully. You can search Windows PowerShell ISE shortcut in start menu or simply type ise in PowerShell to open editor. There are few other ways to open PowerShell ISE that you guys can explore your own ;).

Disable Debug.Assert ui popup message setting

We add debug.assert to our code for debugging purpose. But sometime in a server with many sessions this pop-up message won’t come up and our application processing stuck there waiting for us to click on assert message. On way to find that assert message is login to server as a console. To disable permanently, add following setting to web.config or app.config file of your application to disable debug.assert pop-up message.

<assert assertuienabled=”false” />

View GAC assemblies in Windows Explorer

Follow below steps to enable windows explorer view for GAC assemblies:

  1. Disable the namespace extension. To do that rename the Shfusion.dll to Shfusion_Old.dll which is located in the %windir%\Microsoft.NET\Framework\vx.x.xxxx folder (e.g. v2.0.50727)
  2. Open %windir%\assembly folder. This time you will get the windows explorer view.
  3. Rename the Shfusion_Old.dll to Shfusion.dll to enable the namespace extension. Now open %windir%\assembly you will get the standard view.

Cheers !!!

%d bloggers like this: