Windows 7 PowerShell Scripts

PowerShell Scripts for Windows 7PowerShell for Windows 7

Here is a collection of real-life scripts for you to gain experience of using the PowerShell command line.

Windows 7 PowerShell Scripts


PowerShell Scripts to Delete Windows 7 Temp Files

Let us build up gradually, learn a little about PowerShell before we actually delete any files.

Get-ChildItem $Env:temp -recurse

Note 0: $Env is a built-in Environmental variable.  -recurse tells Get-ChildItem to search the sub-directories.

Note 00:  If you need help launching PowerShell in Windows 7 see here.

Example 1: Count Your Temp Files 

$List = Get-Childitem $Env:temp -recurse | Where-Object {$_.extension -eq ".tmp"}
Foreach ($_ in $List ){$
$count = $count +1}
"Number of files " +$count

Note 1a:  $List is a variable I created.  Trace how the output of Get-ChildItem becomes the input of Where-Object thanks to PowerShell’s (|) pipelining.

Note 1b: This Windows 7 PowerShell script also contains a Foreach loop, an essential technique for automating scripts.

Example 2: Actually Delete Your Temp Files

We are now ready and prepared for the potent Remove-Item cmdlet.

Get-ChildItem $env:Temp | Remove-Item -recurse -force

Note 2a: Don’t worry if you get some access denied messages.  But do try run Example 1 again and see how the script vastly reduced the number of temp files.

Windows 7 PowerShell WMI Scripts

One reason to learn how to write PowerShell scripts rather than use the GUI is that you can access areas where there is no Windows 7 GUI, or the information is spread inconveniently over 2 or 3 menus.

Example 3: WMI ComputerSystem

Get-WmiObject win32_computersystem

Looking at Windows Management Instrumentation (WMI) is rather like using a microscope to observe what’s happening in the operating system. 

Guy Recommends: WMI Monitor and It’s 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

Example 4: PowerShell Script to Interrogate the NIC

Get-WmiObject Win32_NetworkAdapterConfiguration

Let us research a PowerShell’s cmdlet with Get-Member

Get-WmiObject Win32_NetworkAdapterConfiguration | GM

Note 3a: GM is an alias or shorthand of Get-Member

Now we will put knowledge of the properties to work and create a useful PowerShell script.

Get-WmiObject Win32_NetworkAdapterConfiguration | `
Format-Table IPAddress, MACAddress, Description, DHCPEnabled -autosize

Note 3b: PowerShell’s backtick (`)allows the command to wrap over onto the second line, put another way, without the backtick PowerShell would assume two separate commands and the script would fail.

Windows 8 PowerShell scripts will be very similar.

Display Error Messages In Your Windows 7 Event Logs

In this scenario you want to a PowerShell script to display all the latest error messages, but you are not sure if the problem is recorded in the System or Application log.

Example 5: Get-Eventlog with $_.

Get-Eventlog System -newest 2000 | Where {$_.entryType -match "Error"}
Write-Host "Application Log"
Get-Eventlog Application -newest 2000 | Where {$_.entryType -match "Error"}

Note 5a: Observe the neat $_. construction.  It means in the current pipeline, that is any Errors from Get-Enventlog’s output.

See more on Windows 7 and PowerShell cmdlets.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

Example 6: Control Windows 7 Restore Points

This Windows 7 PowerShell script will disable, or enable the operating system’s restore points.  This script will show you how to create a PowerShell function. 

Set-SysRestore Function

When you create a PowerShell functions it’s best to begin its name with an existing verbs such as ‘get-‘ or ‘set-‘.  Observe at the heart of this function is the switch ($RestoreOpt), which in turn calls on the WMI class systemrestore.  It is this parameter which provides your option to enable or disable the system restore.

One important point, you need to supply a different value for $sysName.

## Author: Dave Stone, Brooks Automation
## Date: 6 May 2011
## Enables or disables the system restore on a Windows 7 workstation
## Usage Set-SysRestore option computername

Function Set-SysRestore {
$RestoreOpt = $(throw "Please specify option, enable or disable"),
$sysName = $(throw "Please specify IP Address or Windows 7 computer name.")
switch ($RestoreOpt)
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
Set-SysRestore -RestoreOpt enable -SysName

Note 6a:  This is a PowerShell scripts where you need to ‘Run as Administrator’.  Windows 7 PowerShell Script

Note 6b:  Check the two options for the -RestoreOpt parameter.  It’s the last line which puts the function Set-SysRestore to work, and enables the system restore on the C:\.

Note 6c:  To turn off your restore, issue the reverse command:
Set-SysRestore -RestoreOpt disable -SysName YourComputer

Set-SysRestore -RestoreOpt disable -SysName Win7Computer

Note 6d:  Pay close attention to -SysName, your Windows 7 or Vista machine is unlikely to have an IP address of  Alternatively, script the hostname.

Summary of Windows 7 PowerShell Scripts

I hope that these real-life examples of PowerShell in action will achieve the following, give you a top-down understanding of what is possible, give you a feel for the Verb-Noun cmdlets, and how you can join them with a simple pipe (|).

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


Microsoft Windows Version 7 Configuration Topics