Windows PowerShell Examples of Real Life Tasks

Windows PowerShell Examples of Real Life Tasks

This section is dedicated to putting PowerShell into action.  I have selected topics to illustrate how PowerShell can help you with real life tasks.

 ♣

Contents of My PowerShell Example Pages

Each of the above topics has its own ‘how to’ instructions and also ‘Learning points’.  As a result you will be able to modify my examples to suit your situation. 

If you are new to PowerShell I suggest that you start by going to the Control Panel, Programs and 'Turn Windows feature on'.  With Vista you need to download PowerShell from Microsoft's site.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

PowerShell Real-life Examples: Functions

What separates the beginner, who is just dabbling, from the intermediate who wants to learn how to get the most from PowerShell, is the willingness to turn an ordinary script into a reusable function.

Function to Check if Device Drivers Are Signed
I created this function to find out if my device drivers had been signed.  At the heart of the function is a built-in Windows command called DriverQuery.

Create a to Display Diskspace Numbers
It's not easy to read numbers such as 3295002640005, so I created a PowerShell function to handle the conversion to 3GB. 

How to Display an IP Address Using PowerShell
I designed this function to display IP addresses.  The point is that you no longer have to use cmd to run IPConfig and similar programs, this is how you can not only get the information in PowerShell, but improve on the old DOS Box.

See PowerShell function Get-IPConfig »

Real Life PowerShell Example: Get-Eventlog Remote EventID

PowerShell’s Get-Eventlog is tricky to operate.  What makes it easier is focussing on the parameters, especially -Logname, and for remoting you will need -Computer.  Once you get the basics working there is a wealth of techniques and properties you can apply to this most versatile cmdlet.

Scenario: You need to investigate a particular EventID. 
Important:  Amend my value of -lt ‘100’ to -eq ‘YourNumber’.  Do remember the speech marks.
Optional: Change "LocalHost" to "YourNetworkMachine"

# PowerShell Remote EventLog example with specific EventID
Clear-Host
$Machine = "LocalHost"
Get-Eventlog -Logname System -computer $Machine -newest 1000 `
| Where-Object {$_.EventId -lt ‘100’} `
| Format-Table MachineName, Source, EventID -auto

Note 1: Please change -lt to -eq, and ‘100’ to the EventID you are researching.

Note 2: The above script is ready for remoting, just change the value of $Machine variable.

Note 3: See more about Eventlogs

Guy Recommends:  SolarWinds’ Log & Event Management ToolSolarwinds Log and Event Management Tool

LEM will alert you to problems such as when a key application on a particular server is unavailable.  It can also detect when services have stopped, or if there is a network latency problem.  Perhaps this log and event management tool’s most interesting ability is to take corrective action, for example by restarting services, or isolating the source of a maleware attack.

Yet perhaps the killer reason why people use LEM is for its compliance capability, with a little help from you, it will ensure that your organization complies with industry standards such as CISP or FERPA.  LEM is a really smart application that can make correlations between data in different logs, then use its built-in logic to take corrective action, to restart services, or thwart potential security breaches – give LEM a whirl.

Download your FREE trial of SolarWinds Log & Event Management tool.

PowerShell Measure-Object

Measure is one of PowerShell’s more unusual cmdlets; it deals with maths such as sum, average and maximum.  I find this cmdlet useful for improving the speed of my scripts.

Example  Measure-Object Counting Files
There various methods of employing PowerShell to count the files in a folder; here is one using Measure-Object.

# Measure-Object counts Windows System 32 files
Get-ChildItem C:\Windows\System32 | Measure-Object

This cmdlet reminds us of the concept of an object, and thus how this cmdlet can also measure the properties of text objects, such as the number of words in a document.

See more uses of Measure-Object »

Other Windows PowerShell Sections

If you like this page then please share it with your friends

 


See more Windows PowerShell sections

PowerShell Home   • Introduction  • Dreams   • 3 Key Commands   • Free Permissions Analyzer

PowerShell Real-life Examples   • PowerShell Techniques   • PowerShell Syntax   • Contact

PowerShell ISE  • PowerShell 2.0   • PowerShell v 3.0   • PowerShell v 4.0   • PowerShell v 4.0

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.