Windows PowerShell for Vista Introduction
My mission is to help you install Microsoft’s PowerShell on Vista. Once you have installed PowerShell, the first task is to set the script execution policy:
- Help Set-Executionpolicy
- Set-Executionpolicy remotesigned
- Get-Executionpolicy
Introduction to Windows PowerShell Vista Topics
- 7 Things to get you started with PowerShell
- How to Launch PowerShell in Vista
- Try out a few PowerShell Commands
- Windows 7 PowerShell Scripts
- Windows 8 PowerShell Scripts
- PowerShell v 3.0
♣
7 Things To Get You Started With PowerShell
- Where to Download PowerShell (Get OS specific version)
- Begin with the old dos commands (dir, CD, Ipconfig) – Just to get comfortable
- Look for the Verb-Noun Pair (Set-executionpolicy)
- Call for help (Get-Help wmiobject)
- Create Scripts (Cmdlets)
- Deploy Pipe symbol | (Join commands | filter)
- Master $_. It means the current object in the pipeline. (Where {$_.name -Contains "Microsoft})
How to Launch PowerShell in Vista
- Download the correct version of PowerShell from Microsoft’s site. Naturally for Vista, choose the Vista x86 or x64 version and avoid XP or Windows Server 2003 versions.
- Install the PowerShell file with the .msu extension, mine was called:
Windows6.0-KB928439-x86.msu. - Find the PowerShell file! I tried Search ‘PowerShell’. This revealed a shortcut to the executable in this folder:
Start Menu –> Programs –> Windows PowerShell ISE (v 2.0) - The underlying executable, called PowerShell.exe was found in:
%Windir%\\System32\WindowsPowerShell\v1.0 (note the final sub-directory). - With PowerShell, my favorite thing to do, is to run cmdlets. These are nothing but PowerShell commands in a text file, but saved with .ps1 extension.
- Problem 1: By default, you cannot ‘run’ or execute .ps1 files. This script restriction for security reasons.
Solution 1: You can change the ExecutionPolicy from restricted to RemoteSigned (best) or Unrestricted. - Problem 2: If you run PowerShell normally, you don’t have the permission (rights) to change the ExecutionPolicy.
Solution 2: Run PowerShell as an Administrator. It’s simplest to accept this rather than argue – that you are already logged on as THE ADMINISTRATOR. What I do to get through this security restriction is:
a) Create a shortcut to PowerShell.
b) Right-click the shortcut, select Properties, Shortcut (tab) and then Advanced (button)
c) Tick the box: Run as Administrator. - Launch PowerShell as Administrator, run this command:
Set-ExecutionPolicy remotesigned (note there is no minus sign in front of remotesigned). See more on Set-ExecutionPolicy. - Now you can run cmdlets, which are those instruction files with a ps1 file extension.
Guy Recommends: Free WMI Monitor for PowerShell
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 PowerShell 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
Feel The Control of Vista’s PowerShell
In this section of my website I am going to focus on getting you started with Microsoft’s PowerShell. Before I give you more examples, I want to say to developers and programmers that PowerShell has all the control and agility of languages such as perl, ruby and python. One clue of PowerShell’s object nature is that you first need to install .NET Framework 2.0, otherwise PowerShell cannot work. Above all else, PowerShell gives administrators the power and versatility of the UNIX Bash command line.
My most difficult concept to convey is an act of faith. Trust me that PowerShell will deliver the goods and exceed your expectations. Here is an example of what PowerShell could do; imagine this scenario: ‘One of my servers is running slower than the rest, can I check the version numbers of all the processes?’. You are unlikely to find a built-in tool to answer this question, however, if you are expert in PowerShell you will be able to build cmdlets to answer this and similar operating system questions.
Programmers and developers will soon create scripts or cmdlets, yet my task is to persuade those with just server administration skills that they too can create code to interrogate the operating system. Naturally, Admins will use the time-honoured tradition of copying other people’s scripts, indeed one of my goals is to create a bank of such cmdlets. My greatest challenge is to provide a middle way, to persuade Admins that they can decipher the commands and easily modify the code to suit their own situations.
My Challenge – To Get You Started
My goal is to get you started with PowerShell. My challenge is that in thirty minutes you will be able to perform all tasks that you currently achieve at the CMD prompt, and start issuing new PowerShell commands that are just not possible with CMD.EXE.
Assumption, you have successfully installed PowerShell and .NET Framework 2.0. Begin by clicking Start, Run and type PowerShell, alternatively, create a short cut to PowerShell in the Quick Launch area.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Try Out a Few PowerShell Commands
Normally, when people move to another scripting language their old trusty commands don’t work in the new shell. Well, if you migrate from cmd.exe to PowerShell, then cd and dir are available thanks to Aliases. In a short time, you adapt to the new ways of doing the same things. For example, set-location is the equivalent of cd, and Get-Childitem the Alias for dir. In addition, PowerShell allows you to run all the native Windows executables such as Ipconfig, Tracert and NetDiag.
The miracle of PowerShell is that thanks to Aliases, those coming from UNIX, perl or .NET can make a similar transition but employing their much more high powered commands.
Principle 1: The heart of PowerShell is always a Verb-Noun pair. You start with verb add a hyphen then finish with a noun. For example, set-location is the equivalent to cd (change direcory) in DOS. Incidentally, the reason cd works in PowerShell is because it has a built in Alias called cd which maps to set-location. Good news, you can create your own aliases.
PowerShell Introduction – Simple Examples
I realize that these are Windows rather than Exchange 2007 examples but I want to get you up and running:
Preamble, Start, Run PowerShell. In the dark blue shell type:
# PowerShell Vista
Get-Process
# or
Get-Eventlog system
Principle 2: Create lots of scripts or cmdlets. Save your text commands in a text file with a .ps1 extension, for example ProcessAll.ps1. Moreover, save that file into PowerShell’s working directory. Then execute these cmdlets from within the Microsoft Shell, for example: .\ProcessAll. Your two key characters are the dot and the backslash. The secret is get into the rhythm: dot, backslash then filename. There is no need to add the .ps1 extension, also avoid spaces.
If all else fails just type the complete path:
C:\Documents and Settings\GUYT\Documents\PSConfiguration\ProcessAll
(Or D:\scripts\ProcessAll)
To save typing and risk making a spelling mistake use the Tab key to invoke auto-completion.
Auto-completion examples
Get-ev [tab] auto-completes to –> Get-Eventlog
set-lo [tab] auto-completes to –> set-location
Example of a PowerShell Cmdlet
# This cmdlet generates a report about memory in active processes
#
"Report generated at " + (Get-date)
"" # insert blank line
"High memory usage (>100000000)"
Get-Process | where-object { $_.VirtualMemorySize -gt 100000000 }
Help For PowerShell in Vista
All that PowerShell v 1.0 had was the command-line. However PowerShell 2.0 has the ISE, my best tip is to seek out this GUI version.
PowerShell’s help is clear, concise and full of useful examples. Put your faith in PowerShell’s help. Cast aside negative experiences of other help files. Overcome your pride and ask for help. All you need to query the vast help information store, is either precede the item with ‘help’, for example, help Get-Member; or else append -? for example, Get-Process -? (hyphen question mark). The executable powershell.exe even has its own help, at the PS prompt, type: powershell -?
Another useful sister cmdlet is: Get-Command, which lists the built-in cmdlets. Better still, try the * wildcard:
Get-Command *service.
Who is Windows PowerShell Designed For?
®