PowerShellPracticeAndStyle icon indicating copy to clipboard operation
PowerShellPracticeAndStyle copied to clipboard

Profile scripting best practices

Open rkeithhill opened this issue 10 years ago • 4 comments
trafficstars

I'm not sure where this belongs or if it even belongs in this repo but... Most of us probably have pretty complex profile scripts. My PSReadline custom configuration alone is over 200 lines. So I've taken to moving those "module-specific" configurations out into a separate file that sits by my profile.ps1 file. It is called "PSReadline_config.ps1" - I've seen this approach with Bash (.bash_aliases). I was thinking something like <ModuleOrDescription>_config.ps1. Then in your profile script you just dot source it in like so:

. $PSScriptRoot\PSReadline_config.ps1

This has made it easier for me to share my PSReadline config with others.

BTW there is probably some best practices around "which" profile scripts to use. I'm old school and use the current user all hosts script and for PSReadline I check against the host name. I should probably use the console's profile Microsoft.PowerShell_profile.ps and ditch the if ($host.Name -eq 'ConsoleHost') check.

rkeithhill avatar Jun 25 '15 17:06 rkeithhill

I tend to dot source my profiles anyway. I usually keep them in onedrive and then my normal profile just dot sources the onedrive ones. I should probably just investigate using junctions or something instead.

I do like the idea and naming you have used.

gerane avatar Jun 03 '16 12:06 gerane

I can't think where to put this, but it's worth discussing ... and documenting.

Jaykul avatar Jul 14 '16 19:07 Jaykul

I also have one for Posh-git - Posh-Git_config.ps1.

rkeithhill avatar Jul 14 '16 20:07 rkeithhill

Posh-Git definitely needs one.

This would make for a lot of utility, especially if people are writing generally-useful things to put in their profile and are releasing them as scripts on the gallery.

michaeltlombardi avatar Jul 15 '16 12:07 michaeltlombardi