Showing posts with label profile. Show all posts
Showing posts with label profile. Show all posts

Tuesday, June 17, 2014

PowerShell Profile Files – What's in Yours?

I've recently finished teaching a PowerShell course – and during the course we got into a long discussion on Profiles and how to best use them. With 4 or more (think ISE vs. Console, etc) profile files to pick from, what to you put into your profile file and which ones do you use? We had a lively discussion and decided that for the most part,just using Profile (this host, current user) is probably easiest as most systems have just one user logging in and that user (i.e. me/you) tends to just need two profiles (one for console and one for ISE).

But that leaves the question: what should I put into my profile. I just noticed a good Scripting Guys post showing what a number of Microsoft employees use in their profile files. This makes good reading, provides food for thought and has given me ideas of things I want to add to my profile. Happy reading.

Thursday, January 17, 2013

PowerShell Profile Files

As a power user of PowerShell, I have heavily customised my environment, through the use of profile files. I create additional PS drives, define key variables and aliases, create credential objects for my test labs and local domain, configure the title bar to make the user I’m running PowerShell as a bit clearer, etc.  My ISE profile file also imports a few modules which add menus to the ISE console. I rely on my profiles – and even when teaching. I’ve put up a base console and ISE profile on my DropBox account and have loaded the basic files there.

But when it comes to running background jobs or doing remoting, those profile files are not, as it were, in play. Mike Pfeiffer, over on Don Jones (old) blog, writes an interesting article about this very point. While the post dates from 2011, the relevance of profiles is all the more these days as remoting becomes much more prevalent (and the ‘missing’ profiles become more of an irritation!).

This article is worth a read!

 

Saturday, April 17, 2010

PowerShell Profile Files

PowerShell defines some special script files, called Profiles, that you can use to customise and configure a PowerShell Session, whether you are using Powershell.exe, PoweShellISE.exe or a customised version (e.g. Exchange Management Console). The neat thing about the profile is that it runs, at start-up of every PowerShell host, in dot sourced mode. Thus functions and variables defined in the profile persist in your shell.

For each PowerShell Host, you have up to four potential profiles:

  • AllUsersAllHosts
    • C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
  • AllUsersCurrentHost
    • C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1 (PowerShellISE.exe), or
    • C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 (PowerShell.exe)
  • CurrentUserAllHosts
    • C:\Users\<username>\Documents\WindowsPowerShell\profile.ps1
  • CurrentUserCurrentHost
    • C:\Users\tfl\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 (PowerShellISE.,exe), or
    • C:\Users\tfl\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 (PowerShell.exe).

For both the CurrentHost files – these will vary depending on which host the script is run. Above shows the profile files for both PowerShell and PowerShell ISE. The profile files (obviously one each of the four!) run in the order noted above. This means the admin could, for example define some functions in the AllUsersAllHosts that you can override in your CurrentUser profiles.

As it turns out, there’s a very PowerShell one-liner you can run to return your host’s profile files (and can even tell you if the file exists!) This one-liner looks like this:

$profile | gm *Host* | % { $_.Name } | % { $p = @{}; $p.Name = $_ ;$p.Path = $profile.$_; $p.Exists= (Test-Path $profile.$_); New-Object PSObject -Property $p } | Format-Table –AutoSize

I’ve posted a similar script over on my PowerShell Scripts blog. The script I’ve posted on the PowerShell Scripts blog contains a Format-List (to look a bit better on the blog. You might prefer the Format-Table in the one-liner above.

 

Technorati Tags: ,