Powershell: Getting Started (Part 1)

How to install

Powershell is shipped with Windows so it can be run without any prior installations.
Simply open the Start menu and type powershell. If you see an option with (x86) on the end this means this is the 32 bit version of the application and if you have an option which is just “Windows PowerShell” this will be the 64 bit version and the version you should select as your device is 64 bit.

This post is based from watching the following tutorial.

Syntax

The syntax of PowerShell cmdlets (pronounced commandlets) is verb-noun. See an example below:

 Get-Date

Cmdlets perform actions and typically return an object to the next command in the pipeline.* Passing objects to the pipeline will be covered later on.
Cmdlets are also case-insensitive so the following cmdlet is also valid:

get-date

The three most valuable cmdlets in PowerShell

Getting started with PowerShell automatically becomes less daunting when you know the following cmdlets.

Get-Command

This cmdlet will assist you in finding cmdlets.

Get-Command *

This will return a list of all the base commands available in PowerShell. The * usually means a wildcard in many languages so you can use it with other words such as:

get-command *date

This returns the following

CommandType   Name       Definition
-----------   ----       ----------
Cmdlet        Get-Date   Get-Date [[-Date] <DateTime>] [-Year <Int32>] [-...
Cmdlet        Set-Date   Set-Date [-Date] <DateTime> [-DisplayHint <Displ...

as both cmdlets have any string before the word ‘date’.

You can use this cmdlet to find any cmdlet when used with the wildcard (*).

Get-Help

What does that cmdlet do? Using the Get-Help cmdlet will assist you in understanding the cmdlet. It’s syntax is Get-Help cmdlet
For example:

Get-Help Get-Date

Get-Member

As previously mentioned , cmdlets typically return an object. The get-member cmdlet gets the members, the properties and methods, of objects *. The example below pipes the date object returned from the get-date cmdlet to the get-member cmdlet.

get-date | get-member


   TypeName: System.DateTime

Name                 MemberType     Definition
----                 ----------     ----------
Add                  Method         System.DateTime Add(System.TimeSpan value)
AddDays              Method         System.DateTime AddDays(double value)
AddHours             Method         System.DateTime AddHours(double value)
AddMilliseconds      Method         System.DateTime AddMilliseconds(double value)
AddMinutes           Method         System.DateTime AddMinutes(double value)
AddMonths            Method         System.DateTime AddMonths(int months)
AddSeconds           Method         System.DateTime AddSeconds(double value)
AddTicks             Method         System.DateTime AddTicks(long value)
AddYears             Method         System.DateTime AddYears(int value)
CompareTo            Method         int CompareTo(System.Object value), int CompareTo(System.DateTime value)
Equals               Method         bool Equals(System.Object value), bool Equals(System.DateTime value)
GetDateTimeFormats   Method         string[] GetDateTimeFormats(), string[] GetDateTimeFormats(System.IFormatProvide...
GetHashCode          Method         int GetHashCode()
GetType              Method         type GetType()
GetTypeCode          Method         System.TypeCode GetTypeCode()
IsDaylightSavingTime Method         bool IsDaylightSavingTime()
Subtract             Method         System.TimeSpan Subtract(System.DateTime value), System.DateTime Subtract(System...
ToBinary             Method         long ToBinary()
ToFileTime           Method         long ToFileTime()
ToFileTimeUtc        Method         long ToFileTimeUtc()
ToLocalTime          Method         System.DateTime ToLocalTime()
ToLongDateString     Method         string ToLongDateString()
ToLongTimeString     Method         string ToLongTimeString()
ToOADate             Method         double ToOADate()
ToShortDateString    Method         string ToShortDateString()
ToShortTimeString    Method         string ToShortTimeString()
ToString             Method         string ToString(), string ToString(string format), string ToString(System.IForma...
ToUniversalTime      Method         System.DateTime ToUniversalTime()
DisplayHint          NoteProperty   Microsoft.PowerShell.Commands.DisplayHintType DisplayHint=DateTime
Date                 Property       System.DateTime Date {get;}
Day                  Property       System.Int32 Day {get;}
DayOfWeek            Property       System.DayOfWeek DayOfWeek {get;}
DayOfYear            Property       System.Int32 DayOfYear {get;}
Hour                 Property       System.Int32 Hour {get;}
Kind                 Property       System.DateTimeKind Kind {get;}
Millisecond          Property       System.Int32 Millisecond {get;}
Minute               Property       System.Int32 Minute {get;}
Month                Property       System.Int32 Month {get;}
Second               Property       System.Int32 Second {get;}
Ticks                Property       System.Int64 Ticks {get;}
TimeOfDay            Property       System.TimeSpan TimeOfDay {get;}
Year                 Property       System.Int32 Year {get;}
DateTime             ScriptProperty System.Object DateTime {get=if ((& { Set-StrictMode -Version 1; $this.DisplayHin...

This shows all the members the object contains and although all of these are not displayed, they can be using the following commands.

Other useful cmdlets

get-date | select -expand Year
2019

This cmdlet’s syntax is cmdlet | select -expand member and this retrieved the year from the date that was returned from the get-date cmdlet.

The above cmdlet returns a specific member from the object. The below example returns all the properties of the object.

Get-Date | Format-List


DisplayHint : DateTime
Date        : 17/12/2019 00:00:00
Day         : 17
DayOfWeek   : Tuesday
DayOfYear   : 351
Hour        : 1
Kind        : Local
Millisecond : 814
Minute      : 33
Month       : 12
Second      : 25
Ticks       : 637121432058143549
TimeOfDay   : 01:33:25.8143549
Year        : 2019
DateTime    : 17 December 2019 01:33:25

The syntax of this cmdlet is cmdlet | format-list

Leave a comment

Design a site like this with WordPress.com
Get started