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.

%d bloggers like this: