GSX Blog

PowerShell v5 and Tips & Tricks from the experts

Posted by Olivier Raynaut on Tue, Jul 01, 2014

Once again it was with great pleasure that I have attended to some session presented by Jeffrey Snover and Don Jones, two of my favorite PowerShell influencers. The room was packed with IT Pros and developers, ready to hear the latest announcements about PowerShell along with some tips and tricks from these two experts.

First they showed us the latest features that should be out very soon from Windows Management Framework v5, or PowerShell v5 if you prefer ;)

For those of you that cannot wait until then you can try the latest WMF v5 Preview available here.

OneGet

The WMF team has released a new package management system called OneGet. Basically this new module allows to work with multiple repositories that will allow you to easily find and install new packages from anywhere. This technology is based on Nuget and could be seen as something similar to apt-get for linux. You can use packages from trusted repository or private ones, so even build your own.

Once you have gotten the OneGet module you can see the available cmdlets part of it:

                Get-Command –module OneGet

Get Command –module OneGet

You can browse the package source using “Find-Package”, and then install any package using “Install-Package”, quite straight forward right?

                Install-Package zoomit -verbose

 Install Package zoomit verbose

These new features should allow easier collaboration and faster enhancements around the PowerShell community. Jeffrey also mentioned that release cycle will be much more flexible as they are no more waiting for major OS updates to release new WMF versions. Part of improving the PowerShell community, Don Jones advised the crowd to upload their scripts on PowerShell.org for peer review so that everybody could benefit from any written script, and the community itself could improve their scripting skills.

 

Tips and Tricks

>Help

No PowerShell session can start without talking about Get-Help. Jeffrey shared that it was by far his favorite cmdlet as it allows you to find any information you need. Moreover help files are now updated on a regular basis and you can update it by using the “Update-Help” cmdlet. Should the server not have any Internet access it is possible to use the “SourcePath” parameter to access offline help that would have been previously downloaded though the “Save-help” cmdlet.

A good trick that I learned there is that if you want to keep the help opened while executing the cmdlet you can use the “showWindow” parameter as follows:

                Get-Help Get-Process –showWindow

 Get Help Get Process –showWindow resized 600

>Size

Ever tried to calculate what was 5Gb in Kb so that you could input it in a PowerShell cmdlet? Well that was much harder than this:

                Get-Disk | where {$_.size -gt 230Gb}

 Use SizeParametersValues resized 600

 

>Pipeline Variables

If you do not like the $_ variable (really, who does?) you can use the “PipelineVariable” before any pipe in order to improve readability:

                Get-Process -PipelineVariable Process | where {$Process.Name -like "*svc*"}

 Pipeline Variables resized 600

>Best Practises and Patterns

  • Comment your code with relevant explanations
  • Performance: depends what you are doing, always test your code
    • Piping is often slower but uses less memory
    • Structured syntax is often faster but may use more memory
    • Aesthetics
      • Indent the code
      • Avoid backticks for line breakers, prefer “;” or “|”.
        • A nice trick is to use a variable to store parameters
          • $params = @{‘parameter1’ = ‘value1’}
          • Verb-noun @params
          • Avoid “write-host”, prefer write-verbose, -warning, -debug
            • Unless you are writing a “show-“ command
            • Think about whether you are writing a tool or a controller
              • Tool
                • Modular
                • Re-usable
                • Output raw data
                  • Look at: Get-Process | select –first 1 –property *
  • Controllers
      • Use one or more tools to automate a business process
      • Are single-purpose
      • May not have a cmdlet-style name
      • Output formatted data
  • Use source control to version and backups your scripts

 

Find out more about the PowerShell by watching the recordings of PowerShell session during TechEd 2014: http://powershell.org/wp/2014/05/15/teched-n-a-2014-session-recordings/

Tags: Microsoft TechEd, Exchange monitoring, Powershell, Best practices, Management Framework