I Have a PowerShell Profile—Now What?

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about the what to add into a Windows PowerShell profile. Microsoft Scripting Guy, Ed Wilson, is here. It was a beautiful sunny weekend in central Florida. The Scripting Wife and I headed to Tampa, where we met up with various Windows PowerShell peeps. It is good to get together and talk about cool stuff with cool people. The Microsoft office in Tampa is a really cool place to hang out, and it has awesome views of the harbor. There are also lots of really good restaurants in town. Anyway, when I am around Windows PowerShell people, one of the things I tend to ask them is what they have in their profiles. The answers are usually pretty interesting. In addition to providing good ideas, their answers also lend insight into how people use Windows PowerShell and the kinds of things they do with it. I like to organize my Windows PowerShell profile into sections because it helps me keep track of stuff. As you may know, a Windows PowerShell profile is sort of like the old-fashioned autoexec.bat file. It is simply a file that has a special name and resides in a special place. Unlike the autoexec.bat file, however, there are different files that can be in different places, and that will have different scope. This can become really confusing if one does not do a little bit of planning at the outset. There are really six profiles that are available by default:

  • All user profiles for all hosts
  • All user profiles for current host
  • Current user profiles for current host
  • Current user profiles for all hosts

In addition, the current host profiles exist for the Windows PowerShell console and the Windows PowerShell ISE. If I type $profile, I get the current user/current host profile location. So, if I type $profile in the Windows PowerShell console, it points to one location, and if I type $profile in the Windows PowerShell ISE, it points to another location: This is shown here:

PS C:\> $host.name

ConsoleHost

PS C:\> $PROFILE

C:\Users\ed\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PS C:\> $Host.Name

Windows PowerShell ISE Host

PS C:\> $profile

C:\Users\ed\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 If I want to affect something in my Windows PowerShell experience, for both the Windows PowerShell console and the Windows PowerShell ISE, I have two choices:

  • I can copy the same code to both profiles that I listed previously.
  • I can simply copy it once to the current user ALL host profile, which located here:

PS C:\> $PROFILE.CurrentUserAllHosts

C:\Users\ed\Documents\WindowsPowerShell\profile.ps1 This is a design decision that I will talk about later. For now, let’s work with the current user/current host profile for the Windows PowerShell console, that is $profile when it is typed in the Windows PowerShell console. I generally put in these four types of things in my Windows PowerShell profile:

  • Aliases
  • Functions
  • PSDrives
  • Commands

In yesterday’s post, All About PowerShell Profiles, I added two commands:

Set-Location C:\ ~and

Start-Transcript These commands would go into a section of my profile that I call #Commands. Here is a blank Windows PowerShell profile:

#——————————————————————————

#

# PowerShell console profile

# ed wilson, msft

#

# NOTES: contains five types of things: aliases, functions, psdrives,

# variables, and commands.

# version 1.0

# 7/20/2015

# HSG 7-21-2015

#——————————————————————————

#Aliases

#Functions

# Variables

#PS_Drives

#Commands When I use my template for my profile, I simply add the things I want to appear in the profile. One of the first things I like to do is to add a function to edit my profile. I like to use the Windows PowerShell ISE to do this, so the command is simple:

function edit-profile {ise $profile}. I then like to create an alias for the function. EP seems to be a good choice for an alias for my “edit profile” function, so I use the Set-Alias cmdlet to do that.

Note  The PSCX module has an EP alias, and it has a function Edit-Profile. But their function uses Notepad instead of the ISE, so I like mine instead. Because my profile loads before their module, my alias and function takes preference over theirs on my system. I like to create a few variables, such as an easy variable that points to my Documents folder. I call it Doc, and I use the New-Variable cmdlet to create that. I also like a module drive for my personal modules. I pick up the path from the $psmodulehome environmental variable. I split it and take the first element. As you can see here, my personal profile is coming along in terms of functionality:

#——————————————————————————

#

# PowerShell console profile

# ed wilson, msft

#

# NOTES: contains five types of things: aliases, functions, psdrives,

# variables and commands.

# version 1.0

# 7/20/2015

# HSG 7-21-2015

#——————————————————————————

#Aliases

Set-Alias -Name ep -Value edit-profile | out-null

#Functions

Function Edit-Profile

{ ISE $profile }

#Variables

New-Variable -Name doc -Value “$home\documents”

#PS_Drives

New-PSDrive -Name Mod -Root ($env:PSModulePath -split ‘;’)[0] `

 -PSProvider FileSystem | out-null

#Commands

Set-Location c:\

Start-Transcript Now you have an introduction to using Windows PowerShell profiles. Profile Week will continue tomorrow when I will talk about more cool stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon