Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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):
. . .

Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

Microsoft Delivers Service Pack 2 Beta 2 for Vista, Server 2008

Microsoft on Tuesday announced the availability of the Beta 2 version of Service Pack 2 (SP2) for Windows Vista and Windows Server 2008. Since both operating systems were developed from the same code base, they have a common servicing structure and thus ...


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


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing