PowerShell Environment customizing $Profile
$Profile is a PowerShell built-in variable. It actually points to a script file Microsoft.PowerShell_profile.ps1 which will be run every time you start PowerShell. It's more or less like .bashrc or .zshrc in the Linux/Unix world.
The main purpose is to customize your PowerShell Session, for example with custom aliases or additional ps-drives without effecting the configuration on the host side. It's pretty simple and effective.
Create your $Profile script
Usually the script file does not exist, so we are going to check it first.
If the output is as expected "false", we will create it. Or if its yes, we will replace it to get rid of unexpected behaviors.
The script file will be located here:
C:\Users\%USERNAME%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Edit your $Profile script
You can use notepad or PowerShell itself for editing the $Profile script.
Notepad:
PowerShell(the cooler way ;)):
@"
Write-Host "~~~~~~~~~~~~~~~~~~ Welcome Master! ~~~~~~~~~~~~~~~~~~" -foregroundcolor yellow
Set-Location -Path 'C:\'
Import-Module ActiveDirectory
Set-Alias ga Get-AdGroup
Write-Host "~~~~~~~~~~~~~~~~ Config done Master! ~~~~~~~~~~~~~~~~" -foregroundcolor yellow
"@ | Add-Content -Path $Profile
After editing done, restart PowerShell. Thats it!
Tips
- Avoid putting commands which takes long time to execute, this will slow you down
- Backup your $Profile script before doing major changes
- If your using OneDrive Sync, your $Profile script could be used on several machines
PowerShell's $PROFILE is an underrated powerhouse for personalization and productivity. Whether you're a casual scripter or a PowerShell pro, tweaking your profile can save time and give your shell superpowers
Cheers!