Sunday, September 14, 2008

Windows Vista and Windows PowerShell

Windows PowerShell is Microsoft's new command-line shell and scripting language designed specifically to improve the manageability of Windows and products running on Windows such as Exchange Server 2007. If you haven't heard of Windows PowerShell before, think of it as an integrated version of the Windows Command Prompt (cmd.exe) and VBScript that is easy to use and will allow you to automate and control system administration tasks.

I asked the PowerShell team to put together some examples that show how PowerShell makes it easy for IT Pros to manage Windows Vista. Wow! I had no idea that PowerShell was this awesome. I think you will find that PowerShell will really jumpstart the productivity of a lot of IT pros as well power users who like to tweak their systems. To try any of the following tasks, you simply need to install Windows PowerShell on your Vista computer.

12 Cool Features of Windows PowerShell
By David Aiken, PowerShell Architect Evangelist and Jeffrey Snover, Windows PowerShell Architect. David Aiken has also recorded a video of these examples that you can view online on his Channel 9 DFO Show.

1. Built-in Cmdlets (pronounced "commandlets") for Managing Windows
All Cmdlets in Windows PowerShell follow a verb-noun syntax such as get-service, get-process, stop-service, get-wmiobject. Type get-command at the prompt to see the over 130 cmdlets provided by Windows PowerShell.

To get all services, type:

PS> get-service

2. The power of wildcards and working objects.
To get all services that start with "w" and then also get all dependent services associated with these services, simply type:

PS> get-service w* | format-list DisplayName, DependentServices

3. Whatif you could test your commands before committing to them.
Windows PowerShell has a unique feature called Whatif that will tell you the result of the command without executing the command.

The following command tells you which services starting with "w3" would be stopped. This utility is addictive once you start using it as it allows you to explore the features of PowerShell without causing any harm.

PS> stop-service w3* -whatif

Windows PowerShell Whatif

4. Take a transcript
PowerShell allows you to start and stop transcripts of all your commands. This makes it easy to test commands and simultaneously save them for use in a script.

PS> Start-Transcript -Path c:\demo\dfoshow.txt
PS> Stop-Transcript

5. Make Windows talk from the command line.
Because Windows PowerShell is optimized to work with objects it is easy to access COM objects as well as the .NET framework from the command line. The commands below tell your Vista machine to pronounce the words "Windows Vista and PowerShell". Substitute your favorite phrase. Talk about easy command line access.

PS> $spVoice = new-object -com "SAPI.spvoice"
PS> $spVoice.Speak("Windows Vista and PowerShell")

6. Using Windows PowerShell to access applications such as Windows Media Player 11.
The following commands - which can easily be put in a script - will play a song by the band The Posies in Windows Media Player. This is a trivial example (see ScriptCenter for more WM11 examples) but demonstrates how Windows PowerShell provides comprehensive command line access to applications.

PS> $band = "The Posies"
PS> $player = New-object -com wmplayer.ocx
PS> $playlist = $player.mediacollection.getbyauthor($band)
PS> $player.openPlayer($playlist.item(0).sourceurl)

7. Windows PowerShell as a command line storage calculator
PowerShell allows you to complete basic calculations from the command line.

PS> 2*2

But PowerShell will also allow you to quickly solve storage problems. For instance, how many 700MB cds are needed to backup 11GB?

PS> 11gb/700mb

Or, how many terabytes (1000 GB) of storage is needed to backup 425 Vista desktops with 320gb storage each?

PS > (320gb*425)/1000GB

8. Using PowerShell as a calendar calculator
To find out how many days until the New Year, simply type:

PS> ([DateTime]"1/1/2007" -[datetime]::now).days

Using PowerShell as a calendar calculator
Using Windows PowerShell to manage Windows Vista: Files, WinSAT, UAC, and Bitlocker
If you are an IT Professional and you are evaluating new Windows Vista functionality, you should also evaluate Windows PowerShell. Here are some examples to get you started.

9. How many files of type X do I have on my machine?
Windows Vista has many new file types for event logs, group policy files, etc. .PS1 is the extension for Windows PowerShell scripts. Here is a command that will return the number of VBScript files, Bat files and PowerShell Scripts in a directory and its subdirectories.

PS> dir -include *.vbs, *.ps1, *.bat -recurse | group-object extension -noelement

10. Collecting Windows System Assessment Tool data from the command line.
The Windows System Assessment Tool (WSAT) provides numeric ratings (1= bad, 5=good) of system performance for processor, disk, graphics, etc so you can get a summary and potential solutions for improving performance. Because this data is stored in WMI, Windows PowerShell can programmatically collect this data from multiple computers and allow you to quickly evaluate the health of a set of machines without having to log in to each one. Here is a command to get WSAT data from a single Vista machine and format it in a nice, auto-sized table for viewing. Also an example of a PowerGadgets chart.

PS> get-wmiobject win32_winsat | format-table __SERVER, *SCORE -autosize
PS> get-wmiobject win32_winsat | select *score | out-chart -Title "System Assessment Scores by PowerGadgets"

Collecting Windows System Assessment Tool data from the command line

11. Configuring User Account Control
Windows Vista's User Account Control (UAC) helps improve security by requiring that all programs run in standard user mode by default, rather than with administrator privileges. Some IT pros have asked if they can temporarily disable the prompts if they need to do a sequence of administrative tasks in a row. The following example can be used to temporarily disable the UAC prompt, and easily turn it back on. A value of 0 will turn off the Prompt Behavior of UAC on Vista. With a Value of 2, the UAC prompt will be turned back on.

PS> set-itemproperty -path HKLM:\SOFTWARE\MICROSOFT\WINDOWS\CurrentVersion\Policies\System -name ConsentPromptBehaviorAdmin -value 0

12. Managing BitLocker with PowerShell
Vista's BitLocker feature helps prevents data loss via encryption. A thief who attempts to use another operating system or run a software hacking tool is prevented from overriding Windows Vista file and system protections or performing offline viewing of the files stored on the protected drive if BitLocker is installed. Here is how you can view, disable and enable BitLocker features from the Windows PowerShell prompt. (For this example, Windows BitLocker needs to be enabled on the c: drive.)

PS > $drives = get-wmiobject -namespace root\CIMv2\Security\MicrosoftVolumeEncryption -class Win32_EncryptableVolume
PS> $drives | format-table DriveLetter, PersistentVolumeID -autosize
PS> $BitLockDrive = $drives[0]
PS> $BitLockDrive.GetProtectionStatus()
PS> $BitLockDrive.DisableKeyProtectors()
PS> $BitLockDrive.EnableKeyProtectors()

One customer already using PowerShell is MySpace. It's been so useful for them that they are already using Windows PowerShell in their production environment to manage thousands of Windows Servers. Ad-hoc system administration tasks that used to take MySpace 10 minutes now take them only seconds.

A number of Microsoft partners have also provided Windows PowerShell-based tools designed specifically for Windows Vista. FullArmor announced Windows PowerShell cmdlets that improve the manageability of Group Policy on Vista. /n Software announced a beta of their free PowerShell-based network management tools. And PowerGadgets has built amazing charting and gauge utilities including gadgets that easily integrate with the Windows Vista Sidebar. These gadgets allow Windows Vista users and application developers to easily visualize system or application data - such as sales numbers or website performance -- and other line-of-business relevant data visualization needs.

This was a long post, but we were only able to scratch the surface of what PowerShell can do. For more comprehensive info see, the PowerShell team blog and the TechNet PowerShell ScriptCenter.

No comments: