Windows 8 PowerShell Scripts

PowerShell Scripts for Windows 8PowerShell for Windows 8

Do you get frustrated by long drill-downs before you find the setting you are looking for?  If this happens to you, then why not learn how to type a handful of PowerShell commands to obtain all the information you are seeking quickly and easily.

Examples of PowerShell Scripts for Windows 8

Here are a collection of interesting scripts for you to learn how to use the PowerShell command-line in Windows 8.

 ♦

Launching PowerShellWindows 8 PowerShell Script Examples

To get started just type ‘PowerShell’ in the Windows 8 Search box.  I always select the ISE (Integrated Scripting Engine) version so that I can store, amend, and experiment with my scripts easily.

PowerShell Script to Delete Temp Files

~tmp files have a habit of hanging around even in Window 8.  It’s always worth having a clear-out of these old temporary files.

Example 1: Count Your Temp Files 

Let us experiment with PowerShell cmdlets before we actually remove any temporary files.

Get-ChildItem $Env:temp -recurse

Note 1a:  $Env is a built-in Environmental variable.  -recurse tells Get-ChildItem to  trawl through the sub-directories under temp.

Clear-Host
$Count=0
$FileList = Get-Childitem $Env:temp -recurse | Where-Object {$_.extension -eq ".tmp"}
Foreach ($_ in $FileList ){$_.name
$Count = $Count +1}
"Number of files " +$Count

Note 1b:  $FileList is a variable I created for this PowerShell example.  Trace how the output of Get-ChildItem becomes the input of Where-Object, and how this transfer is controlled by PowerShell’s (|) pipelining.

Note 1c: This Windows 8 PowerShell script also contains a loop.  Foreach is but one of the looping technique for automating scripts.

Example 2:  Time to Delete Your Temp Files

We now ready to introduce the Remove-Item cmdlet, keep in mind there is no ‘Delete’ verb, instead PowerShell consistently uses ‘Remove’.

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

Note 2a: Don’t worry if you get ‘Access denied’ error messages.  However, do rerun Example 1 and confirm that this PowerShell script has vastly reduced the number of Windows 8 temp files.

Windows 8 PowerShell WMI Scripts

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

Example 3: WMI ComputerSystem

Looking at Windows Management Instrumentation (WMI) is rather like using a probe to see what’s happening deep within the Windows 8 operating system.

Get-WmiObject Win32_Computersystem

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

Get-WmiObject Win32_ComputerSystem | GM

Note 3a: GM is an alias for the Get-Member cmdlet.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

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 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

Example 4: PowerShell Script to Check Your Computer

From the properties of Win32_ComputerSystem we can check whether your computer specification is as you expected.

Get-WmiObject Win32_ComputerSystem | `
Format-Table name, NumberOfLogicalProcessors, TotalPhysicalMemory -auto

Note 4a: PowerShell’s backtick (`) instructs the first line to wrap over onto the second line.  The converse, without the backtick PowerShell would assume two separate commands and the script would fail.

Test a Website’s Response Time

Here is just a bit of fun; PowerShell tackles a real-life task of measuring a website’s response time.

Example 5: PowerShell Does Math

$Avg = 0
$Server = "www.computerperformance.co.uk"
$PingSite = Test-Connection -count 3 $Server
$Avg = ($PingSite | Measure-Object ResponseTime -average)
$Calc = [System.Math]::Round($Avg.average)
Write-host "Average response time to $Server is $Calc ms"

Note 6a: Test-Connection is a built-in PowerShell command which mimics ‘Ping’.

Note 6b: Observe how the results are piped (|) into Measure-Object.

Example 6: Exchange and Windows 8 PowerShell

One of the jobs of a PowerShell script even on a Windows 8 laptop is to run Exchange cmdlets and interogate, or even configure, your Exchange Server; here are examples:

We are going to check the information of one particular user:

Get-Mailbox -Identity "Guy Thomas"

Here we need to get information about a particular Exchange mailstore database:

Get-Mailbox -Database Exch01

Note : Naturally you need to be logged on with an account which has the appropriate Exchange Server roles.

Exchange Monitor from SolarWindsGuy Recommends: The SolarWinds Exchange Monitor

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 freebie, as part of their commitment to supporting the network management community.

Free Download of SolarWinds Exchange Monitor

Three Essential PowerShell Commands

For researching any PowerShell project all you need is to call for at least one of these classic cmdlets:

  1. Get-Help
  2. Get-Command
  3. Get-Member

1) Get-Help

By default each PowerShell cmdlet returns only basic information; consequently, the most useful parameters are often hidden.  One way of extending your repertoire of switches is by prefixing the name of the Verb-Noun you are currently using with Get-Help.

Let us use Get-Date as an test vehicle.  On its own Get-Date does what you may expect, it returns the current date and time.  But suppose you wish to script the  time-zone offset?

#Research parameters with Get-Help
Get-Help Get-Date -full

Result of extra knowledge

Get-Date -uFormat "%A Time zone %Z"

Note 1a:  There are two points to remember with Get-Help, firstly, there is no need for the | pipeline, and secondly, I always append -full because I like examples of the cmdlet in use.

Note 1b:  Studying Get-Date’s description and examples reveals useful parameters such as -uFormat

Challenge: Investigate each of these commands by prefixing each with Get-Help

  • Get-ChildItem
  • Get-Process
  • Get-Eventlog

2) Get-Command

Run without modifiers, Get-Command lists all PowerShell’s 400+ cmdlets.  When I research a task I often have an idea of the command that I am looking for, so I employ the asterisk (*) judiciously.

Imagine the scenario, you want to create a Windows 8 PowerShell script instead of using the Task Manager, the problem is  that you see the Process tab in the Task Manger GUI you don’t know the name of the corresponding PowerShell cmdlet.

Get-Command  # This gives too much information!

# Try each of these in turn

Get-Command *proc*

Get-Command -Noun Process

Note 2: You can even apply Get-Help to Get-Command, this how I discovered the -noun and parameter.

3) Get-Member

Most PowerShell objects has more properties and methods than you can ever imagine.  To access this ‘hidden’ store of scripting tools never miss a chance to pipe a cmdlet, or object into: | Get-Member.  I will be surprised if you don’t find at least one property or method that will improve your original Windows 8 script.

Get-Process

Get-Process | Get-Member

Note 3a:  Even Get-Member has parameters.  If you investigate with Get-Help you will find a useful switch, or to use its correct name, parameter called -MemberType.  This is how it filters properties and omits methods from the result.

Get-Process | Get-Member -MemberType property

Note 3b:  You really do need the pipe (|).

Here is an example script of how this technique may bear fruit.  Let us imagine that we are interested in these properties: Handles, PriorityClass, or StartTime, then this is how you modify the output of Get-Process.

# Explicit example of adding properties
Clear-Host
Get-Process | Format-Table Name, Handles, PriorityClass, StartTime -auto

Note 3c: This example employs Format-Table to display the results, and more importantly, to select the best properties to list in the output’s columns.

Challenge:  Research with -MemberType, then amend my example so that it displays different properties.

See the latest on Update-Help »

Summary of PowerShell Scripts for Windows 8

PowerShell mimics what you can do in a Windows 8 GUI.  The benefit is that with a script you can issue commands quicker, and with greater precision, that by clicking through the corresponding Windows menus.

  • Remember that he basic PowerShell unit is a verb-noun cmdlet.
  • PowerShell’s signature tune is | which is known as the pipeline.  The concept is that the output of the first command becomes the input of the second, or third pipeline.
  • Employ Get-Member to research methods and properties, which you can use to refine your Windows 8 script.

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

 


Microsoft Windows 8 PowerShell Topics

Windows 8 Overview  • Windows 8 PowerShell  • Win 8 PowerShell Scripts   • Free WMI Monitor

Windows 8 Registry  • Windows 8 Registry Hacks  • Activate Administrator Windows 8

Win 8 Registry  • Windows 8 AutoRun  • Review WMI Monitor  • SolarWinds Mobile App Manager