Windows Tip Sheet

All Your Eggs in One Basket

Make any script in your PowerShell profile script available to all your sessions via dot-sourcing.

Here’s another quick tip on how to use your PowerShell profile script. PowerShell by default looks for %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_Profile.ps1 and runs it when found. But don’t feel you have to put all your PowerShell eggs in one basket. You can call other scripts from within your profile:

#Microsoft.PowerShell_Profile.ps1
Set-location C:\Scripts
#call other scripts
. c:\scripts\posh\psinclude.ps1
. c:\scripts\posh\myfunctions.ps1
. c:\scripts\posh\myprompt.ps1
#end profile

It may be hard to tell from the formatting, but there is a period and a space before each script name. This is referred to as “dot-sourcing”. When a script is dot-sourced any of its functions are loaded into PowerShell’s global scope which makes them available throughout your PowerShell session.

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

By utilizing this technique, my profile script is now very short. All the other functionality I might need during a PowerShell session has been modularized into other scripts. I can update those scripts as needed without having to make any modifications to my primary profile script.

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