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: ,

No comments: