Determining Your Version of PowerShell & Upgrading.md - Juan-bit94/Ops401D10 GitHub Wiki

Determining Your Version of PowerShell

How to do that

  • On your Windows OS, click on the start menu button, and type PowerShell.
  • After you find it, right click and Run as administrator.
  • PowerShell is not like other command line interfaces or CLIs.
  • To determine the version, you have to run the following command: $psversiontable.psversion
  • After running the command, you should see tables breaking down the version.

Upgrading to PowerShell 5.1

  • Lets say that you have a different version of PS and you want to get the latest one (5.1). This how to guide will help you install the Windows Management Framework 5.1 along with PowerShell 5.1.
  • On the Windows OS, go to the browser and search for WMF 5.1 and click on the very first link (should be a download link).
    • Its important that before you download and install it, make sure your system meets the requirements (click on the system requirements tab).
  • after downloading the WMF 5.1, open it to run the program, and then it will upgrade.

What's New with PowerShell?

  • Various new features extending PS's capability.
  • Control and provision complex Windows-based environments
  • Backwards compatible:
    • All processes from previous versions should perform without changes
  • Classes
    • Similar syntax as object oriented language structure
    • Class and Enum along with other keywords have been added
  • Write-Host cmdlet:
    • Defines how PS handles informational stream for data
    • Wrapper for Write-information
    • computable with workloads.

Provisions

  • OneGet
    • New module provisions the ability of installing packages from Chocolatey repo
  • Chocolate
    • Package Manager for Windows
    • Goal is to provide a framework for installing applications and tools for Windows
  • Uses PowerShell to provision NuGet packages
  • Benefits of OneGet:
    • List and manage software repositories for compatible packages
    • Install and uninstall packages from repositories.

Benefits of 5.0 Framework

  • Package Management cmdlets
  • Network Switch cmdlets
  • Provides updates for Windows PowerShell and PowerShell ISE, including:
    • Desired State Configuration (DSC)
    • Windows Remote Management (WinRM)
    • Windows Management Instrumentation (WMI)

PowerShell Best Practices

  • Using Proper Data Validation Techniques, this means to validate actions in script to ensure the values being passed are accurate.
  • Error Handling:
    • Forecasting errors that may appear and writing code to prevent any additional issues
    • Use of wrapping message in a try catch block to provide the user with a descriptive message regarding the issue.
  • Limit the Use of Aliases
    • When creating large scripts, often difficult to track aliases
    • Use full cmdlet name to make it easier to read the code for all users
    • Example:
      • Aliases: ls|?{$_.Name -eq 'John'}|gc
      • Full cmdlet name: Get-ChildItem | Where-Object {$PSItem.Name -eg 'John} | Get-Content
  • Frequent Use of Commenting in Large Scripts
    • It gets very easy to lose touch when searching through thousands of lines of code.
    • Create comments in more areas of your script
    • Script header
      • A detailed header can provide useful search to locate specific areas of script
      • Inserting comments into the script body can be useful to document what various parts of the script do.
  • Resist Using Write-Host
    • Write-Host is the only cmdlet that writes directly to console instead of a pipeline limiting its functionality.
  • Write Proper and Clean Code
    • Messy code can be hard to read; not indenting code can make things very difficult to debug for errors, if poorly formatted.
    • Make heavy use of tabs, spaces, and carriage returns.
  • Frequent Use of Write-Verbose and Write-Debug
    • Using quotes for comments only benefits the user reading the code.
    • Using Write-Verbose and Write-Debug can assist the user for debugging.
  • Use Single Quotes
    • PowerShell searches for back ticks, escape characters, and escape in double quotes.
    • PowerShell also searches for characters after a string character ($) indicating a value using double quote
    • Using single quotes prevents this from happening
  • Use Source Control
    • Using version control will assist in managing previous versions or controlling updates if several occur in a short period of time
    • Assists groups of users who are working on specific scripts at the same time.
  • Avoid Using Hungarian Notation
    • In the past when VBScript was heavily used, this was common practice: $strMonitor $intCount
  • PowerShell uses a different system and would confuse the common PowerShell developer who is not accustomed to VBScript. Example of PowerShell: ** $Monitorname** $cp_count

What is WMF (Windows Management Framework)?

  • Driver and delivery mechanism.
    • Provisioning a consistent interface across various versions of Windows and Windows Server
    • Allows the compatibility of installing updates to versions (up to 2 lower versions) below the current one.
  • What's new with 5.0?
    • Functionality updated from WMF 4.0
    • Only available for Windows 7 SP1 and 8.1, Windows Server 2008 R2, 2012, and 2012 R2
  • Installation instructions
    • Can be installed via command prompt, Control Panel, or .msu, and many depend on Windows type, version, service packs, and KB updates.