Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


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):
. . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

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

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


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

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement