Tag: PowerShell

Learning PowerShell – Using PowerShell Community Extensions

ps The PowerShell Community Extensions are a set of additional cmdlets, providers, functions and scripts which the community asked for and have been written for us to use and take advantage of.

Once you download PSCX you’ll want to add this module to something called your PowerShell Module path, this is the place you’d normally put your PowerShell Modules so that you can import them for using in your scripts. To check what your PowerShell Module path is from within PowerShell type:-

$env:PSModulePath

Add PSCX to your $env:PSModulePath so you can import it and use it anytime you start using PowerShell, to add PSCX type the following:-

$env:PSModulePath = $env:PSModulePath + “;C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;”

the last part should be where you chose to install PSCX.

Ok so no we have this added to our PowerShell Module path lets see if we can start using it.

To check what PowerShell modules are already imported type:-

Get-Module

The screen shot below shows me which modules have been imported:-

get-module

To add PSCX you need to import the module, so lets do that by typing:-

Import-Module PSCX

And that’s it we can call any of the PSCX cmdlets from our scripts, the list of which are extensive and include:-

Write-Zip
Format-XML
Get-XML
UnBlock-File

and many more

Hopefully this is enough to whet your appetite.




Learn PowerShell – Piping

Piping in PowerShell is awesomesauce and best explained by some examples, but firstly a quick explanation of piping, lets say we want to get a list of files from the c:\windows folder on your machine, order them by the last modified date and select the first 50 files and output the list to a text file in the current folder, simple requirements and here it is:-

Set-Location c:\windows
Get-ChildItem | Sort-Object LastWriteTime -Descending | Select-Object -First 50| Out-File files.txt

  • Set-Location is the cmdlet which is roughly the equivalent to the cd command found in a command prompt.
  • Get-ChiLdItem is the cmdlet which is roughly the equivalent to the dir command found in a command prompt, but you can use it with objects, lists and so on.
  • Sort-Object is the cmdlet to sort obviously and can be used against your objects, lists and much more.
  • Select-Object tis the cmdlet to select a number of objects (files, records, results), you can use this with -first, -last, -skip and much more
  • Out-File will is the cmdlet to simply write output to a file.

Obviously a very simple example but you take A pass the results to B, B then does work on it, passes it to C and you got the idea. This is a how piping works and can yield the data your looking for quickly and in very little code indeed.

PowerShell Blog Posts




Learn Powershell – ExecutionPolicy

psCheck what the script execution policy is currently set to on your system:-

Get-ExecutionPolicy

The options are AllSigned, ByPass, Default, Remote Signed, Restricted or undefined

You can read about each of these by typing in the following command:-

Get-Help Set-ExecutionPolicy -online

This will load the online help for this command, if your ever needing help with PowerShell then you can search for help like so, Get-Help *services*, this will show you what commandLet’s are available for working with Services, i.e. starting them stopping them and so on.

I set the execution policy to RemoteSigned so that I can run my own PowerShell scripts but ones I download from the internet wont run unless I change the setting, so that my system is secure from any unwanted side effects from other peoples scripts.

PowerShell Blog Posts




PowerShell useful Tools

ps

As part of my blog post series on learning PowerShell I thought it might be a good time to list the tools I have came across and give them a shout out as I reckon the list of tools have been very helpful in learning some good practices when writing PowerShell scripts.

Lets get to the list:-

  • PowerShell ISE Steroids – this is a great add-on for PowerShell ISE, its not free but you get 10 days to check it out and I recommend you do.
  • PowerShell Script Browser Script Browser for Windows PowerShell ISE enables you to search for script samples in the TechNet Script Center, invaluable tool in my opinion for looking at example scripts
  • Power GUI PowerShell Editor with a few nice extras

I will add more as I come across them 😉




Learn PowerShell – Getting Started

ps

To Start PowerShell, click Start, and type in PowerShell, choose Windows PowerShell ISE and run this as an Administrator (right click).

Tip:-  To check which version of PowerShell you have type $PSVersionTable, this will show a table of results and your looking for the top one which is the PSVersion, if this is 2.0 then you really should update before we go any further. At work I have been using PowerShell 3.0 and now 4.0 is out.

To update to version 3.0 the download link is PowerShell 3
To update to version 4.0 the download link is PowerShell 4

Tip:- PowerShell has in-built help which is really great, make sure its up to date by typing in Update-Help

PowerShell Blog Posts




Adventures into Powershell

psOver the last couple of months I have been working with PowerShell on a daily basis, I had initially looked at PowerShell at home a little bit and thought yeah its fairly cool and left it at that, if you haven’t looked at PowerShell and want to learn some quick tips for getting started then stick around.

At work I was tasked with doing a small project using PowerShell and I can now report that I think PowerShell is pretty awesome, I have even found myself thinking you could do that using PowerShell.

The learning curve for PowerShell is not that high and if you put a little bit of effort into learning the basics it starts to become pretty easy to do most things.

This will be the first post covering a number of topics within PowerShell and I will cover what I have been learning as we go.