April 16, 2008

Customizing Windows PowerShell's Internal Functions

Tailor PowerShell's predefined functions to your unique work habits
RSS
View this exclusive article with VIP access -- click here to join |
See More Scripting Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Executive Summary:
You can redefine, change through aliasing, or delete Windows PowerShell's internal functions to create the best work environment for your needs. Add your customizations to a profile script to make them persistent.

Several PowerShell commands are actually functions predefined by an internal script that runs when PowerShell starts up. However, you can freely redefine these functions to suit your personal preferences. I'll demonstrate the techniques—redefining, aliasing, and simple deletion—primarily using PowerShell's prompt and more functions; in the process, you'll see some of the situations where redefining functions makes sense. Because the functions we're modifying are automatically created by PowerShell at startup, the changes are only temporary. They'll be reset to the default each time you start PowerShell. So, as a final topic, I'll discuss making the changes happen automatically at PowerShell startup using a PowerShell profile script.

Redefining PowerShell's Prompt Function

PowerShell's prompt is a function invoked each time a command finishes executing. The returned value from the function is used for the next onscreen prompt. The default prompt displays the current PowerShell location. In PowerShell, location is analogous to the working directory for a Windows process, but it isn't precisely the same; your current location might be a registry hive or certificate store. In any case, including the location often makes the prompt extremely long, cluttering the screen with information that I usually don't find valuable.

We can inspect the contents of the prompt function by getting its syntax information (technically, the function's definition) by entering the following command:

(Get-Command prompt).Definition

Figure 1 shows the results you'll see when using PowerShell 1.0. This is the entire internal content of the function named prompt. The location information is inserted by the $(Get-Location) element. There's also a test of the $nestedpromptlevel variable, which I won't discuss; the $nestedpromptlevel test is used to give you a special prompt (>>) when you enter a single statement on multiple lines. To create a new prompt function that removes the location data, you can simply enter the following at a command prompt (or execute it automatically in your PowerShell profile):
. . .

Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Where is Microsoft NetMeeting in Windows XP?

...


Related Articles PowerShell 101, Lesson 1

What You Need to Know to Start Using PowerShell's Personal Profile Scripts

Essential Windows PowerShell Commands

Migrating to PowerShell? Don't Scrap Your Existing Code Just Yet

Related Events PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


SQL Server Magazine Office & SharePoint Pro Windows Dev Pro ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing