Windows 8 PowerShell Scripts
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.
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.
~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.
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.
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'.
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.
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.
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.
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.
From the properties of Win32_ComputerSystem we can check whether your computer specification is as you expected.
Get-WmiObject Win32_ComputerSystem | `
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.
Here is just a bit of fun; PowerShell tackles a real-life task of measuring a website's response time.
$Avg = 0
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.
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.
Three Essential PowerShell Commands
For researching any PowerShell project all you need is to call for at least one of these classic cmdlets:
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
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