Een powershell-profiel is te vergelijken met een autoexec.bat bestand dat we vroeger in MS-DOS hadden. In tegenstelling tot één enkel bestand worden er door powershell meerdere profiel-bestanden gebruikt. Elk powershell-profiel-bestand wordt dan voor verschillende doeleinden gebruikt:
- Alle gebruikers op alle hosts
- Alle gebruikers op de huidige host
- Huidige gebruiker op huidige host
- Huidige gebruiker op alle hosts
Het commando $profile geeft aan welk bestand gebruikt wordt. In het PowerShell Console host:
PS C:\Users\Nico> $profile C:\Users\Nico\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
In PowerShell ISE host wordt een ander bestand gebruikt:
PS C:\WINDOWS\system32> $profile C:\Users\Nico\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
Om het profiel één keer aan te maken en te gebruiken in alle hosts, dus zowel het Powershell-console als in PowerShell ISE, gebruik het volgende:
New-Item -ItemType file -Force -Path $PROFILE.CurrenUserAllHosts
Dit profielbestand wordt aangemaakt als %HOMEPATH%\Documents\WindowsPowerShell\Profile.ps1
ExecutionPolicy
Het is trouwens handig om van te voren de ExecutionPolicy aan te passen. Dit kan in de registry met de volgende sleutel:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
Zet hier een Tekenreeks in genaamd ‘ExecutionPolicy‘ en geef dit bijvoorbeeld de waarde ‘Unrestricted‘
Voorbeeld profiel
Een voorbeeld om in het profiel te zetten is een functie om PowerShell als Administrator te starten en deze functie aan te roepen met een alias, bijvoobeeld ‘sudo’ 🙂
Function Start-ElevatedPowerShell { Start-Process PowerShell -Verb Runas } Set-Alias -Name sudo -Value Start-ElevatedPowerShell | out-Null Set-Location "$HOME\Documents"
Dit zorgt er dus voor dat het commando ‘sudo’ een powershell start als Administrator.
Zonder profiel starten
Mocht je powershell willen starten zonder een profiel, gebruik dan het commando:
PowerShell -noprofile