Go to index

Getting Started

Launch PowerShell

There is nothing new in how to open a PowerShell console, but I want to make you aware that there is a name change in the PowerShell process. The new name is pwsh from the PowerShell Core 6.0 GA release. powershell is the process name for Windows PowerShell and it’s been there since PowerShell v1.0 released. The predominant reason to have a different name for PowerShell Core is to have the Windows PowerShell and PowerShell Core run side by side. There is an in-detailed explanation about the new process name for PowerShell Core by Kevin Marquette , I have given the link to his blog post for more details in the Reference section down below.

Okay, why is it so important to know the PowerShell process name? Yes, you should know the process name to invoke the PowerShell from the command line and to run the automated scripts as well. I have give a Microsoft docs link to refer the pwsh command line options in the Reference section down below.

The image below shows the PowerShell 7 console…

PowerShell CmdLets

Like every other shell has keywords called commands to perform the actions, in PowerShell the commands are called as cmdlets. PowerShell uses cmdlets to perform the required actions. You can use the cmdlets in the interactive shell and in the scripts as well.

PowerShell runtime engine will execute the cmdlet to perform the required action; and some cmdlets perform the action and return nothing, and most of the other cmdlets usually return the output in the form of .Net objects to the next cmdlet in the pipeline (will discuss more in the next sessions) till it reaches the output cmdlet in the pipeline.

CmdLet Overview

PowerShell uses the combination of verb and noun separated by a ‘-’ as its default naming convention, so the name of the cmdlet looks like Verb-Noun, and verb represents the action that the PowerShell can perform and the noun represents on which the action will be performed upon, so if you see the cmdlet names such as Get-ComputerInfo - to get the computer info, Get-Service - to get the services and Get-VM - to get all the Virtual Machines, so they are easy identify and no need memorize much. However, there are some recommended set of standard approved verbs from Microsoft, but the nouns are pretty much you can use any thing. We will discuss more on how to explore the cmdlets and help on how to use in the next session.

In additions to the naming convention, cmdlets accepts parameters to perform the necessary actions based on the parameter values, but that doesn’t mean that all the cmdlets must require the parameters, it can also work without the parameters as well.

Parameters

However, parameters are categorized as ‘Mandatory Parameters’, ‘Optional Parameters’, ‘Dynamic Parameters’ and ‘Common Parameters’

  • Mandatory Parameters - cmdlets, which have mandatory parameters must require to pass the values to it, otherwise those cmdlets will not execute. Mostly other than Get cmdlets will have these mandatory parameters.
  • Optional Parameters - As the name says, these parameters are not mandatory, but adding these parameters will show some difference in the cmdlet actions.
  • Dynamic Parameters - These parameters will be created at the runtime and made available for us to use with the cmdlet, and usually these parameters will dependent on the other parameters.
  • Common Parameters - These parameters are added by PowerShell itself for almost all the cmdlets and the behavior is same for all the cmdlets.

More will be discussed in the next sessions when we will be writing scripts.

Parameter Set

Parameter Set is a combination of parameters or a group of parameters, a cmdlet can have more than one parameter set and at the same time at least one parameter in each parameter set should be unique, and you can’t mix the unique parameters from different parameter sets.

Functions

Besides cmdlets, there are functions as well; so the functions are nothing but set of PowerShell cmdlets with all the valid arguments to achieve something are wrapped up and given a name to it. You can consider these functions are as good as cmdlets and you can notice the same behavior when you are executing the functions, because the functions can also have all the parameters that we discussed in the above. You will get a complete idea when we start working on our own functions.

Aliases

An alias is a short name or a shortcut name of a cmdlet or a function, which you can use the same instead of the actual cmdlet or the function. BY default Microsoft had created some aliases for some cmdlets, however you can create your own aliases. And one cmdlet or a function can have more than one aliases. The more I will explain in the next coming sessions.

Modules

Module is container for cmdlets, functions, aliases, variables, providers and etc., by importing the module, it will add new abilities to PowerShell to perform certain tasks on the target product. Modules are best use to organize the code in the form of cmdlets, functions, aliases and etc., and share it with the other. By default few common modules are imported when you launch the PowerShell.

Run Commands in PowerShell

Launch the PowerShell console and run the commands below. (Refer to the link below in the Reference section to open the PowerShell in case you are brand new to PowerShell)

Know your PowerShell version

To know the PowerShell version, run the command below in the PowerShell console to know the PowerShell version and edition as well.

PS C:\> $PSVersionTable

Output:

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

The above output is from PowerShell 7 and if you notice it is the Core edition and 7.0.0 version, and when you run the same command in the Windows PowerShell then the output will be like this, it is Desktop edition and it’s version is 5.1.

Name                           Value
----                           -----
PSVersion                      5.1.18362.628
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.628
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

How to run cmdlets

As we had already discussed earlier, some of the cmdlets require the parameter(s) and some doesn’t. Now lets see how to run some of the cmdlets…

Without the parameters
PS C:\> Get-Host

Output:

Name             : ConsoleHost
Version          : 7.0.0
InstanceId       : 2724432f-5381-4a83-ac9d-d8f545f21b2a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-IN
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

With the parameters
PS C:\> Get-Process -Name pwsh

Output:

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     61    42.75      77.06       1.08   14460   6 pwsh
     56    41.03      72.52       0.64   19088   6 pwsh
     62    45.97      83.07       2.55   24664   6 pwsh

Uh, more than enough theory, now let’s get started the party!!


Reference

Thank you, and will see you in the next session



Go to index
Last modified: 2 October 2020