Windows Tip Sheet

Profile of a PowerSheller

There'll be times you want to have a partial profile load upon starting PowerShell. Here's a quick way to do that.

Windows PowerShell allows you to customize your shell with your own aliases, functions and extensions. The profile is nothing more than a PowerShell script. There are actually several that PowerShell will look for, but the one that you’ll most likely use is the one found at %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_Profile.ps1. PowerShell will look for this specific file when starting a PowerShell session.

My profile has many custom functions and prompts. However, since I write a lot about PowerShell and test many things, I sometimes need to start PowerShell without loading my complete profile. My solution is to add a prompt at the beginning of the profile script. Here’s what it looks like at the beginning of my profile script:

$ProfileDir="$env:userProfile\My Documents\WindowsPowerShell"
$UserProfile = "$ProfileDir\Microsoft.PowerShell_Profile.ps1"
set-location c:\
$r=Read-Host Do you want to ABORT loading:`n$UserProfile ? `nThe default is NO. [YN]
if ($r -eq "y") {
  Write-Host Aborting profile load
  return
}
Write-Host "loading profile"
#profile continues

The first two lines define variables for my profile. The line with the Read-Host cmdlet is the item I want to show you. When executed, I get a message asking if I want to ABORT loading the profile. I have to answer Y to skip loading it. PowerShell isn’t case-sensitive by nature, so a lower case Y also works. Pressing any other key will continue processing the profile script.

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

I decided to use this approach so that I have to consciously choose to abort loading the profile. Any Powershell commands that are executed before this are still processed so even if I abort loading the profile, my location is still set to C:\. If you have anything you want to always run, put the code before the Read-Host expression.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular