Pluralsight Introduction to PowerShell Course - Notes


Summary of the 2hour 40 minute duration Pluralsight  Introduction to PowerShell course, including other interesting material I found -

+ What is PowerShell?
Windows PowerShell command-line interface is a new command-line tool
It's a scripting language from Microsoft that complements Cmd.exe in the Windows administration context.
Based on .NET
Everything is a .NET object
PowerShell version 2 is included with Windows 7, Windows Server 2008 R2, XP SP3, Windows Server 2003 SP2, Windows Vista SP1
PowerShell scripts have a .ps1 extension

+ How to get started?
To start working with Powershell, go to Windows Accessories & select Windows PowerShell.

PowerShell Environments:
Out of box – PowerShell command window, PowerShell ISE (Integrated Shell Environment)
Free ISE’s – PowerSE, PowerGUI
Commercial

PowerShell ISE or Integrated Scripting Environment is  a GUI for working with PowerShell
You can hide the Script panel in PowerShell ISE if you work a lot in Interactive mode. In the Script pane, you can run either entire script (F5) or run a selection of commands with F8

+ Why learn PowerShell?
In the SharePoint 2010 administration context, Windows PowerShell supersedes the Stsadm.exe administration tool.
Windows PowerShell scripts can also be used to administer other Microsoft server products. This gives administrators a common scripting language across servers.
SQL Server 2008 introduces support for Windows PowerShell.
Windows PowerShell lets SQL Server administrators and developers automate server administration and application deployment.
The Windows PowerShell language supports more complex logic than Transact-SQL scripts, giving SQL Server administrators the ability to build robust administration scripts.
SQL Server cmdlets support actions such as running a sqlcmd script containing Transact-SQL or XQuery statements.
SharePoint 2010 has some 500+ cmdlets.

+ Commands in PowerShell are in the form of Cmdlets (“pronounced Command-lets”).
PowerShell commands have a Verb-Noun syntax. Example:
Get-command – retrieves a list of all system commands that are currently loaded into the PS environment

+ Common verbs:
Get
Set
Out
Start
Stop
Restart
Add

+ Common Nouns:
Help
Command
Service
Computer
Location
Childitems

You can pass parameters, verbs or nouns, to  view cmdlets featuring those keywords
Get-command –verb “get” : will get all cmdlets that have the verb get
Get-command –noun “service” : all cmdlets that have the noun service

To find explanation about at command: get-help get-command
get-help get-command - examples
get-help get-command -detailed
get-help get-command –full

To view environment variables:
Clear-Host
Set-Location env:
Get-children

Show list of snap-ins:
Clear-Host
Get-pssnapin

Show list of registered snap-ins:
Clear-Host
Get-pssnapin –registered

+ What is a PowerShell snap-in?

A PowerShell snap-in is a .NET assembly or set of assemblies that contains cmdlets, providers, type extensions, and format metadata.
All the commands and providers that ship as part of the Windows PowerShell product are implemented as a set of five snap-ins. You can view the list of snap-ins using the get-pssnapin cmdlet.
When a snap-in is loaded in Windows PowerShell, all cmdlets and providers in the snap-in are made available to the user. This model allows administrators to customize the shell by adding or removing snap-ins to achieve precise sets of providers and cmdlets.
PowerShell built-in snap-ins, such as Microsoft.PowerShell.Host, cannot be removed.


+ Aliasing makes it easy for a lot of commands to be mapped to specific PowerShell commands.
Useful for folks transitioning from Linux or DOS. Dir & ls are both mapped to get-childitem, they are both aliases
You can set your own aliases. Example: set-alias list get-childitem # will create a alias called list to display directories & files
The lifetime of this “list” lasts till this PowerShell window is open
It is possible to save your aliases into a file & re-load them. Example: Export-alias c:\ps\myalias.csv list (wildcards also possible)
When you reopen window, use this - import-alias c:\ps\myalias.csv & then use list. You can load a whole bunch of aliases within a csv file & load them when you want to.

+ Pipelining – combining cmdlets for power. Examples:
Get-childitem | where-object {$_.Length –gt 100KB}
$_ represents the current object

Get-childitem | where-object {$_.Length –gt 100KB} | sort-object length

Get-childitem | where-object {$_.Length –gt 100KB} | sort-object Length |
Format-Table -Property Name, Length –AutoSize

Get-childitem | select-object Name, Length
select-object can be used to retrieve certain properties from an object

Get-ChildItem | Where-Object {$_.Name – like “*.ps1”}

+ Provider is a .NET library that provides a standard interface through which we can navigate through whatever object a provider represents.
Providers can extend the list of cmdlets that are available to you.
Get-psprovider command shows a list of providers we have currently loaded in our environment.
Each one of these providers reveals itself to us through the form of Drives. It is through these Drives that we can navigate & retrieve data.

+ Variables:
Get-Variable – displays the variable & its value
Set-Variable – assign a new value to an existing variable
Clear-Variable – clears the contents of a variable. Variable still exists but without any value
Remove-Variable – wipes out a variable.

+ Automatic variables:
$false, $true
$pwd – current directory
$Home – users Home directory
$host – info about a users machine
$PSVersionTable – info about the current version of Powershell
$PID – process ID
$_ - special variable name to represent the current object

+ Strings:
String comparisons are case insensitive by default
To get quotation marks within a string, use mixed quotes or use the quotation mark twice
Here String - for storing large blocks of text, enclose the string within quotes & then precede & terminate quotation marks with @. Lines containing the “@” should exist independently.
Expressions can be used in strings & they need to be wrapped in $(). Example - “There are $((Get-ChildItem).Count) items in the folder $(Get-Location)”
Wildcards & Regular expressions are supported. Example - “Pluralsight” –like “Plural*[s-v]”

+ Arrays:
Arrays in PS are zero-based
$array = “plural”,”sight”
$array = @(“plural”,”sight”)
$array = 1..5  #numeric range notation
The formal array creation syntax is useful when creating a blank array -  $array = @()

+ Hash tables:
$hash = @{“key” = “value”;}
$hash[“mvark”] = “mvark.blogpsot.com”
$hash.Remove(“mvark”) #remove by passing in key
$hash.Contains(“mvark”) #see if key exists
#list keys & values
$hash.Keys
$hash.Values

+ Branching and Looping constructs:
PowerShell supports Branching construct like if-else, switch and Looping constructs like while, do-while, do-until, for, foreach
PowerShell has if & else but doesn’t have if elseif
Switch will match all lines that match. To stop processing once a block is found use break
Switch works with collections, looping & executing for each match
Break – exits the loop on first hit. When used in a nested loop, break exits to the outer loop
Continue – to skip the rest of a loop but go onto the next iteration
Trap – to catch exceptions
Use loop labels to break to a certain loop

+ Script block:
A basic script block is code inside {}. The for (as well as other loops) execute a script block
To put multiple commands on a single line use the ;
You can store script blocks inside a variable
$cool = {Clear-Host; “Powershell and bowties  are cool”}
To run the variable precede it with a &. Example: & $cool
You can use return to return a value. Once it is used, the script exits
"process" is used to pipeline enable a block
Variables declared outside a block are useable inside a block
If you try to change the variable inside a block, PS makes a local copy & uses that, leaving the original alone

+ Functions are basically script blocks with names.
No parentheses or commas required to separate parameters
To add Help to your functions, use custom tags within a comment block and Get-Help can recognize them.

+ Filters are alternatives to functions; work like the Where-Object cmdlet
Filters can be built to remove unwanted files

+ Files:
Contents of a file are stored in a array
Get-Content - can display a file. Get-Content supports wildcards
Set-Content -  write text to a file. It can however be destructive. If a file already exists, it’s overwritten
Add-Content - to append text to a file
User-defined functions can be defined in a .PS1 file & it can be referenced in a script using Import-Module
$psise is a special variable that represents PowerShell Integrated Server Environment
.ps1 can be omitted while running script from the command prompt

+ Other references:
Windows PowerShell Quick Reference
Windows PowerShell Survival Guide

In this course, the presenter Robert Cain has done a good job of covering the basics of PowerShell at a relaxed pace. He has a charming style of delivery & his accent should be easy to follow even by international audiences.

Comments