PowerShell is a wonderful command line equivalent of the Windows
Explorer, taskmanager, or the DOS box.
Instead of drilling down through countless GUI menus you can type a few
Verb-Noun commands and get the job done quicker and with more precision.
What's new in PowerShell 3.0? In a nutshell v 3.0 provides
hundreds of extra
cmdlets. There is also a neat Auto-Complete prompt to help selecting
commands. However, the general improvements compared to v 2.0 are most
useful for developers.
The precice benefits of using PowerShell in Windows 8, rather than DOS or
VBScript, depends on your background. However, for the rest of this
page, I am assuming you have little or no experience of
PowerShell. To newbies I would also like to say this, if you ever
had the desire to be a computer programmer, then thanks to Microsoft's PowerShell you will soon be writing scripts to
interrogate the operating system. As a result, you will not only be able to
get data more efficiently, but also it will make tuning your computer configuration more pleasurable.
One by-product of PowerShell is that pure DOS commands are virtually redundant,
even better, if you do need any of those old
DOS commands, then they work in the PowerShell interface, thus you have nothing
to lose by abandoning cmd.exe and trying
PowerShell ISE with its graphical interface.
My examples underplay PowerShell's abilities as a scripting language,
this is because I don't want to frighten people
from learning
PowerShell, just because they have no knowledge scripting languages such
as C+ or VBScript. My message is this: 'it's ridiculously easy to
get started, but remember, it's hard work to become truly
expert in PowerShell'.
Launching PowerShell in Microsoft's Windows 8 is a trivial task; because unlike Vista, the
.NET Framework and PowerShell binaries are already installed.
From the Metro UI, press the 'p' key. You should see all the
programs and Apps beginning with 'p'.
I recommend right-clicking 'Windows PowerShell ISE', you should see
'Pin' at the bottom right of the sceen.
Pinning means that PowerShell will have a permenant tile on the
Metro UI. I like to drag my PowerShell tile to the left of the
screen so that it's easier to find next time.
Check PowerShell's Version
# PowerShell Version $Host
Let us begin by checking your version. Assuming you have
launched Microsoft's PowerShell, simply type: $Host
Result Name : Windows PowerShell ISE Host
Version : 3.0 [Assuming you have Windows 8]
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
The first point to recognise with PowerShell is the simplicity of the
cmdlet. At the heart of the script is always a Verb-Noun pairing.
Simple Examples:
# Window 8 PowerShell Example Get-Process
or
Clear-Host Get-Eventlog -list
For more ideas on nouns to pair with 'Get' try this:
Get-Command -verb get
Slightly more advanced Windows 8 PowerShell commands
The second point to note with PowerShell is the (|) Pipe symbol.
What this is does is enable the out put of the first cmdlet (Get-Service) to
become the input of the second command (Where-Object)
This Engineer's Toolset v10 provides a comprehensive console of utilities
for troubleshooting computer problems. Guy says it helps me
monitor what's occurring on the network, and each tool teaches me more about how the
underlying system operates.
One example of the new cmdlets, PowerShell 3.0 employs AppX, which is a new method for deploying
applications in Windows 8. Lookout for Microsoft's 'Jupiter' model,
it's designed along the same lines as Silverlight XAP.
Even experienced PowerShell users call for
Get-Help when they need to research a new cmdlet.
# PowerShell Get-Help for Cmdlets Clear-Host Get-Help Get-ChildItem
-full
Note 2: Actually, you don't need 'Get-', plain 'Help Verb-Noun'
works because 'Get' is the default verb.
Talking of assumptions PowerShell assumes that with Get-ChildItem
the first parameter is
for the location, thus you don't need to explicitly use the -path
parameter in most Get-ChildItem scripts.
Note 3: One of the main uses of PowerShell's
Get-Help is for researching -parameters for your task (Some people call them switches).
In this case -recurse, -include and possibly
-force are important modifiers for the Get-ChildItem cmdlet.
Note 4: Whenever I call for Get-Help I append the
-full parameter because I always like to peruse the built-in examples.
PowerShell Tutorial: List
Program .exe Files
Here is an example of a PowerShell script to mimic what you see in the
Windows Explorer. One benefit of this particular script is that it
drills down through all the sub-directories under "Program Files\".
# PowerShell script to list the .exe files in the Program Files
Clear-Host $Dir = Get-Childitem "C:\Program Files\" -recurse # $Dir
|Get-Member $List = $Dir | where {$_.extension -eq ".exe"} $List |
format-Table name
Learning Points
Note 5: Beginning a script with
Clear-Host is my way of clear the screen of any previous output (just as cls does in DOS). The hash symbol # means a remark, or do
not process this line.
Note 6:
$Dir = Get-Childitem C:\Program Files\ -recurse This command sets the variable $Dir to the path of the files that we seek. You have probably guessed the purpose of the -recurse
switch, to drill down to the sub-folders. Get-childitem is often abbreviated to its alias, gci.
Note
7: $List = $Dir | where {$_.extension -eq ".exe"} $List is another variable whose
purpose is to filter the output, as a result we get only executables. Pay
close attention to the construction $_. which means, in this pipeline. Also
note that instead of the
equals sign, PowerShell employs -eq.
Note 8: PowerShell's
signature tune is the pipe symbol (|). Most PowerShell scripts contain at least one pipe to filter the output of the
first command so that it becomes the input of the second clause.
One of the uses of PowerShell even on a Windows 8 laptop is to run
Exchange cmdlets and interogate, or even configure, the Exchange Server;
here are examples:
Home-in on one particular user:
Get-Mailbox -Identity "Guy Thomas"
Or get information about a particular Exchange mailstore database:
Get-Mailbox -Database Exch01
Note 9: Naturally you need to be logged on with an
account which has the appropriate Exchange Server roles.
Here is a
free tool to monitor your Exchange Server. Download and
install the utility, then inspect your mail queues, monitor the Exchange
server's memory, confirm there is enough disk space and check the CPU
utilization. This is the real deal - there is no catch. SolarWinds
provides this fully-functioning product for free, as part of their commitment to
supporting the network management community.
As a beginner, people will tell you that accessing the registry with
PowerShell is as easy as accessing the file system. Guy says that
doing useful work means learning knack. Let start with
PowerShell's PSDrive provider, which opens the door to the registry.
Try this in PowerShell:
CD HKCU:\ (Just as easily as when you type: cd C:\)
The way to get started is to Pin the ISE version to the Metro UI.
To do this just type 'p' and select Windows PowerShell ISE from the list
of Apps.
If you have experience of PowerShell then what's new is more cmdlets, and
more enticements for developers to use PowerShell to manage multiple
servers.
If you like this page then please share it with your friends