Windows PowerShell Methods

Windows PowerShell Methods

This section concentrates on pure PowerShell techniques.  I will explain how to take those first few tentative steps on your journey to master PowerShell.  If you review my techniques below, then you will get an idea of the range of PowerShell methods available.  My aim is to help you develop an approach to writing PowerShell cmdlets (scripts), which suits your learning style.

Topics for PowerShell Methods

I have sequenced these PowerShell techniques.  Thus if you are new to PowerShell I suggest that you start by going to the Control Panel, Progrmas and 'Turn Windows feature on' (or with Vista, downloading PowerShell from Microsoft's site).

  1. Install the correct version of PowerShell
  2. How to Execute Basic Commands
  3. Master PowerShell's Key Commands
    Command and Expression Mode
  4. Get-Member Command
  5. Out-file
  6. Create PowerShell cmdlets
    Use PowerShell's built-in cmdlets
  7. Aliases - Types
  8. Where Statement: $_.name
  9. Loops
  10. WMI Techniques
  11. Functions - How to Create
    Function - Example Plist
  12. Enable Your 'Profile'
  13. Set-ExecutionPolicy
  14. Remote PowerShell v 2.0
  15. Stop-Computer (Shutdown)
  16. Registry Edit
  17. -ErrorAction SilentlyContinue
    Trace-Command
  18. WinRm and WSMan
  19. [System.Math] Functions
  20. Hashtables

 ♣

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Contents of My PowerShell Method Pages

Each topic 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.

Basic Techique: Filtering the OutPut

There are two main methods of filtering the output of a PowerShell command, 'Where-Object' and the -Filter parmeter.

Find .txt Files Using the -Filter Parameter

# PowerShell -Filter Technique
Get-ChildItem "C:\Program Files" -recurse -Filter *.txt

Note 1: # (hash) means: 'Here follows a comment'.

Find .txt Files Using a Where-Object Statement
Not all PowerShell cmdlets support the -Filter parameter, thus it's useful to employ Where-Object.

# PowerShell Where-Object Technique
Clear-Host
Get-ChildItem "C:\Program Files" -recurse | Where {$_.extension -eq ".txt"}

Note 2: Here is PowerShell's signature tune, the Pipe(|).  This means the output of Get-ChildItem, becomes the input of the Where clause.  After a while piping becomes second nature in PowerShell

Note 3: I frequently employ Clear-Host in my examples to provide a fresh slate and thus avoid confusion.

Where-Object is a good place to start »

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 Looping

To me, there is still something magical about scripting loops.  Of course it's this ability to iterate that speeds up scripts and makes it worthwhile taking the trouble to write scripts.

Disk Drive WMI Example

#Example: ForEach Loop
$HardDrive= Get-WmiObject Win32_LogicalDisk
ForEach ($Drive in $HardDrive ) { "Drive = " + $Drive.Name}

Note 1: Observe the two elements. (Condition) {Pay load}

Pure Math Examples
This example uses the 'Do...While' loop.

#Calculate the 13 times multiplication table
$n = 13
$i =1
Do {($i * $n); $i++ } While ($i -le 20)

Note 2: * means multiply

Maths and Text Together
This is not a particularly elegant example, more the rough-and-ready thinking you go through as you learn how to forge PowerShell commands to produce the result you want.

$n = 13
$i =1
Do {Write-Host $i x $n = ($i * $n); $i++ } While ($i -le 20)

Note 3: See how easily PowerShell combines text (x, =) with the numeric variables $n and $i.

See much more on PowerShell Loops »

Other Windows PowerShell Topics

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.

   

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: