Microsoft Certified Professional Magazine OnlineThursday: November 20, 2008 - 21:38 PST
Microsoft Certified Professional Magazine Online About Us | Subscribe to Redmond | Tech Library | Sponsored Webcasts | Get Newsletters
MANAGING THE WINDOWS®  NETWORK MCPmag.com via RSS

Microsoft Unified Communications Virtual Conference and Trade Show: Wednesday, May 21
Facing Active Directory Disasters -- Watch Team Recovery
FREE MAGAZINE SUBSCRIPTION
Get a Free Subscribtion to Redmond magazine in Print or Digital Format!


... Home ... Columns You are reading ... Tips and Tricks ... Recent Articles

COLUMN
Tips and Tricks

Prof. Powershell: Get Smart and Get -Out
PowerShell cmdlets come well-stocked with common parameters. Let's look at what you can get out of the -OutVariable parameter.

by Jeffery Hicks
send e-mail link to article SEND  print article PRINT rate the article RATE AND COMMENT get an RSS feed 
November 2008

Jeffery  Hicks
Just about all PowerShell cmdlets support a set of common parameters. These parameters are the same across all cmdlets regardless of the cmdlet's primary parameters -- which is to say the ones you use the most, like -computername. One common parameter you might want to use is -outvariable.

Usually you save a cmdlet's output to a variable like this:

PS C:\> $svc = get-service
This is perfectly acceptable because you likely plan on using $svc later. But you could also create $svc this way using -outvariable:
PS C:\> get-service -outvariable svc 

-- advertisement --

The only difference is this expression will also write the results to the console. But you'll still have a $svc object. Notice that the name of the variable doesn't need the $. This is because the parameter's value is a variable name and not an object. Here's another way you might use it:

PS C:\> get-service -outvariable svc | where 
	{$_.status -eq "stopped"} -outvariable stopped | 
    Sort Displayname | Select DisplayName
PS C:\> $stopped.count
86
PS C:\> $svc.count
159

This one-line PowerShell expression will produce a sorted list of all stopped services' displaynames, with some added benefits. The results of Get-Service have been saved to $svc. The filtered results from the Where-Object expression have been saved to variable $stopped. Now you can reuse the filtered services variable to work with just stopped services, without having to rerun or retype a long PowerShell command, like this:

PS C:\> $svc | export-
clixml stoppedservices.xml
This is an especially useful technique for long-running commands, where you need information parsed in several ways but don't want to have to rerun a command. Or where the original objects will be lost but you might still need them:
PS C:\> dir d:\files -recurse -outvariable myfiles | 
measure-object length -sum -maximum | 
Select Count,Sum,Maximum

If you want to do more with the files all you have to do is use the $myfiles variable. There's no need to rerun the DIR command, unless you want an updated list:

PS C:\> $myfiles | sort extension | 
   group extension | sort Count -descending | format-table Count,
   Name -autosize
PS C:\> ($myfiles | where {$_.extension -match ".doc"} -outvariable 
   docfiles | measure-object length -sum).sum2809966
PS C:\> $docfiles | Select Fullname

Parameters are all about efficiency. Look at the help file about_CommonParameters for more information.  end article

Jeffery Hicks, MCSE, MCSA, is a Microsoft PowerShell MVP and scripting guru for SAPIEN Technologies. Jeff is a 16 year IT veteran who has co-authored and written several books, courseware and training videos on administrative scripting and automation. He is the co-author of Advanced VBScript for Microsoft Windows Administrators (Microsoft Press) and Windows PowerShell:TFM (SAPIEN Press). His latest is WSH and VBSCript Core:TFM (SAPIEN Press). You can contact Jeffery about "Prof. Powershell: Get Smart and Get -Out" at jhicks@sapien.com.
send e-mail link to article SEND  print article PRINT rate the article RATE AND COMMENT get an RSS feed 



READ MORE: Tips and Tricks

Encryption All Around

With Windows 2003, you can use the Encryption File Systems to give multiple users access to the same encrypted files.

by Derek Melber

January 2005

Help for Lost Files

Windows Server 2003's Volume Shadow Copy Service will help admins get off the restore treadmill.

by Derek Melber

December 2004

Reaching Password Nirvana

Using password filters and longer passphrases will help keep your passwords from being cracked.

by Derek Melber

November 2004

Gaining Group Control

Using GPOs to control local groups on any computer in the AD domain.

by Derek Melber

October 2004

An Easy Fix for a Sticky GPO Security Problem

Ease the pain by automating account creation.

by Derek Melber

September 2004

GPO Treasures

A bevy of often-overlooked settings are available in Group Policy Objects.

by Don Jones

August 2004

DFS to the Rescue

The power of this simple, useful tool is more evident when used during server consolidations or migrations.

by Don Jones

July 2004

Contacts En Masse

Find out how to import Contacts and User Objects into Active Directory, as well as modify batched objects.

by Don Jones

June 2004

Analyze Your Baseline Security

MBSA offers much more than updates to your network.

by Don Jones

May 2004

Remote Shutdown

This script allows logoff and shut down of remote users.

by Don Jones

April 2004

Giving Up Privilege

Learn how to properly manage admin accounts.

by Don Jones

March 2004

Global Catalog Placement

How the GC plays a role in Active Directory.

by Don Jones

February 2004

Want more?...read See the Columns Archive.


TOP


Home | Subscribe/Renew | About Us | Contributors | Tech Library | Tech Library Webcasts | RSS Feeds  RSS Feeds from MCPmag.com
Issue Archive | Blogs | Certification/Careers | Chats | Columns | Now Online | Exam Guide | Events | Features | Forums
MCP Radio | News | Poll | Tech Portals | Press Releases | Reviews | Salary Surveys | Search | Site Map
For more information, e-mail editor@mcpmag.com.
Application Development Trends | Campus Technology | CertCities.com | The Data Warehousing Institute ! E-Gov | EduHound
ENTmag.com | Enterprise Systems | Federal Computer Week | Government Health IT | IT Compliance Institute
MCPmag.com | Recharger | Redmond Developer News | Redmond | Redmond Channel Partner | Redmond Report
TCPmag.com | T.H.E. Journal | TechMentor Conferences | Virtualization Review | Visual Studio Magazine | VSLive!
Copyright 1996-2008 1105 Media, Inc. See our Privacy Policy.
1105 Redmond Media Group
.