Windows PowerShell

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



PowerShell Scripting  -WhatIf and -Confirm

Introduction to PowerShell Scripting -WhatIf and -Confirm

PowerShell's WhatIf and confirm are two great commands for testing complicated scripts without risking the code running amok.  For example, if you decide to delete files by using a script containing wildcards, there could be all manner of unexpected side effects.  By employing PowerShell, and appending -whatIf you get a preview of would happen without risking any damage.

PowerShell WhatIf and -Confirm Topics

 ♣

Mission to Delete Files

Let us take a real life example, we wish to delete files, but because we are using wildcards we are concerned about deleting the wrong type of file.  Instead of gung-ho Guy deleting the files - ready or not, we will take the cautious approach and append -whatIf.  The result is PowerShell completes the command and shows us the result, but does not delete any files.  Incidentally, I cannot find a delete verb in PowerShell, there is however, a remove verb.

PowerShell -whatIf Example

By adding -whatIf at the end of the command we are saying to PowerShell: 'Just test, don't actually make any permanent changes'.  Please note, there could be serious consequences if you don't use the -whatIf switch.  If you don't understand what you are doing, you could delete all your .txt files.

# PowerShell -WhatIf safety parameter
Clear_Host
Get-Childitem C:\SomeFile\*.txt -recurse | remove-Item -whatIf

Note: For safety I chose a fictitious folder, just in case the script ran amok.

A breakdown of what the above script achieves

Get-Childitem  (Rather like dir)
c:\SomeFile (Location to start)
-include *.txt  (The pattern to look for)
-recurse (Search subdirectories)
| remove-Item (The equivalent of Delete)
-whatIf (PowerShell please test, but don't actually complete the operation, in this case, just show me which files with a .txt extension would be deleted if I removed the -whatIf).

Another example of -whatIf

PowerShell -confirm Example

Here is another PowerShell parameter that you append to a 'normal' script - confirm.  It really is a case of confirm by name, and confirm by nature.  PowerShell says to you: 'Do you really want to do this?'

# PowerShell -confirm parameter
Get-Childitem C:\Dzxocs\*.* -include *.txt -recurse | remove-Item -confirm

Note: For safety I chose a fictitious folder, just in case the script went wrong.

The result of -confirm is that PowerShell presents you with choices, however, remember this is now 'live' therefore if you press [Y] or [A] then files will be deleted.

[Y] Yes [A] Yes to all  [N] No  [L] No to all [S] Suspend

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The 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 the tools teach me more about how the system itself operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

How To Research PowerShell Cmdlets Containing -Confirm and -WhatIf

# PowerShell WhatIf cmdlets
Get-command | where { $_.parameters.keys -contains "Confirm"}

Refine Get-Command with -CommandType

# PowerShell WhatIf cmdlets
Get-Command -commandType cmdlet `
| where { $_.parameters.keys -contains "Confirm"} | Format-Table Name

Summary of PowerShell -whatIf and -confirm

Once you have used PowerShell's -confirm or -whatIf you will think, 'Why don't all scripting languages have these safety features'.

See more PowerShell examples for syntax advice

PowerShell Tutorials  • Syntax  • PowerShell functions  • Plist  • RegEx  • -Com

PowerShell -confirm  • WhatIf  • -Match  • -Online  • Where  • -ErrorAction  • -Replace

Please write in 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

WebThis Site

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

 Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.