Microsoft Powershell Basics

Powershell features compared to other traditional shells

Similar features

  • Built in help
  • Pipeline to run commands sequientailly. The output of one command is the input for the next
  • Aliases using alternate names that can be used to run commands

Different features

  • Object over text using objects as inputs and outputs to save time extracting data, formatting, and extracting
  • Cmdlets (commandlets) built on a common runtime to provide a consistent experience in parameter parsing and pipeline behavior taking object input and returning objects
  • Written in .NET core and opensource allowing for using other and building your own cmdlets, scripts, and functions

Cmdlet Basics

Start with the following command

$PSVersionTable
Name                           Value
 ----                           -----
 PSVersion                      7.3.6
 PSEdition                      Core
 GitCommitId                    7.3.6
 OS                             Linux 5.4.0-1058-azure #60~18.04.1-Ubuntu SMP Tue Aug 31 20:34:4…
 Platform                       Unix
 PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
 PSRemotingProtocolVersion      2.3
 SerializationVersion           1.1.0.1
 WSManStackVersion              3.0

The output looks like a table but it is actually an object
You can see this by modifying the command

$PSVersionTable.PSVersion
Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      3      6

You can use a period ( . ) to get access to a specific property, such as PSVersion

Locate commands

A cmdlet is a compiled command developed in the .NET or .NET Core and invoked as a command within powershell.
Cmdlets are named according to a verb-noun naming standard to help you understand what they do and how to search for them. The approved list of verbs is found with the Get-Verb cmdlet.

$Get-Verb

Verb        AliasPrefix Group          Description
----        ----------- -----          -----------
Add         a           Common         Adds a resource to a container, or atta…
Clear       cl          Common         Removes all the resources from a contai…

Searching

To filter the list keep in mind the verb-noun standard. Use flags to target either the verb or the noun in the command you want. The flag you specify expects a value that’s a string. You can add pattern-matching characters to that string to ensure you express that, for example a flag’s value should start or end with a certain string.

  • -Noun flag targets the part of the command name that’s related to the noun. Here’s a typical search for a command that’s related to the noun. Here’s a typical search for a command name using alias as the noun for which we’re searching:
Get-Command -Noun alias*
  • -Verb flat targets the part of the command name that’s related to the verb. You can combine the -Noun flag and the -Verb flag to create an even more detailed search query and type. Here’s an example
Get-Command -Verb Get -Noun alias*
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Alias                                          3.1.0.0    Microsoft.PowerShell.Utility

Locate commands by running the Get-Command cmdlet. This helps you search all the cmdlets installed on your system. Use flags to narrow down your search results to just the cmdlets that fit your scenario.
In this scenario, you’re looking for a cmdlet that can help you work with files

Get-Command -noun File*
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-FileHash                                       7.0.0.0    Microsoft.PowerShell.Utility
Cmdlet          Out-File                                           7.0.0.0    Microsoft.PowerShell.Utility
Cmdlet          Unblock-File                                       7.0.0.0    Microsoft.PowerShell.Utility


Leave a Reply 0

Your email address will not be published. Required fields are marked *