PowerShell Scripts 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.
- PowerShell Script to Delete Temp Files
- Windows 8 PowerShell WMI Scripts
- PowerShell Script to Check Your Computer
- Test a Website’s Response Time
- PowerShell Does Math
- Introduction to PowerShell 3.0
♦
Launching PowerShell
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!
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.
- See more Exchange 2010 PowerShell scripts
- See more Window 8 PowerShell scripts
- See more on Windows 7 and PowerShell cmdlets.
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.
Guy 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
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
®