e-academy – IT training excellence in Cardiff, Newport, Bristol and South Wales

Exploring Windows PowerShell

We take an introductory look at Microsoft Windows PowerShell, (formerly code-named Monad), a new command-line shell and scripting language that helps IT professionals achieve greater productivity and control system administration more easily.

14 November 2007

Windows PowerShell does not require you to migrate your existing scripts, and it is ideally suited for automation of new features in Windows Server 2008.

With more than 130 standard command-line tools, a new admin-focused scripting language, and consistent syntax and utilities, Windows PowerShell accelerates automation of system administration tasks - such as Active Directory, Terminal Server, and Internet Information Server 7.0 - and improves your organisation's ability to address the unique system-management problems of your environment.

Windows PowerShell is easy to adopt, learn, and use - because it does not require a background in programming - and it works with your current IT infrastructure, scripts, and command-line tools.

Here's a quick reference, designed to save you digging around looking for the right syntax. By all means try this on a test virtual machine or similar if you are trying to get to grips with the PowerShell utility.

PowerShell can be used in command-line DOS mode:

PS C:\> md c:\powershell
PS C:\> cd C:\powershell

(to create a file in a directory)

PS C:\powershell> fsutil file createNew c:\powershell\newfile.txt 1000

Or:

PS C:\> ipconfig /all

PS C:\> ipconfig /all >ip.txt

PS C:\> notepad ipconfig.txt

Help:

get-help get-help returns detailed help on getting help!

get-help get-help -examples

get-help get-help -full

get-help about*

get-help get*

get-date

To print out a list of all cmdlets:

get-command

The get-children command is important for drilling down into objects:

used like this:

PS C:\> get-children c:\ it works like dir

get-children | format-list

get-children | format-list -property

Longer cmdlets are abbreviated in Alias to make calling them easier and you can create your own!

get-alias *

get-alias g* (all begining with G)

To return a list of Services and their state:

get-service

What about:

get-process

returns a table but you could try:

get-process | format-list

individual get-process explorer

all the processes starting with T get-process t*

get-process has an alias (there are loads of aliases)

it is:

gps

To look at what if you shutdown a process:

gps | stop-process -whatif

and if you want to confirm the action (i.e be asked "do you want to stop the processes?")

gps | stop-process -whatif -confirm

You would of course say no to this, otherwise you lose all your processes and the box! But what about:

get notepad

notepad starts!

gps notepad | stop-process

notepad shuts!

You can also work with the Registry:

get-psDrive | where {$_.Provider -like "*Registry*"}

(the syntax above i.e {$_.} is the format for using a where clause in any of the get commands)

GCI - path HKLM:\software

(GCI above is the alias for get-childitem)

To return a list of resident hotfixes:

GCI -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix'

or:

GCI -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix'

| where {$_.Name -like "*KB928388*}

To work with variables:

New-Variable administrator

Get-Variable administrator

Set-Variable administrator -value e-academy

Get-Variable administrator (returns e-academy)

Remove-Variable administrator

As you can see, PowerShell is very powerful - and easy to both learn and use. There is much more functionality than we've shown here, so why not download it from the downloads section of the Microsoft Web site and try its functionality for yourself?