So sometimes you run the same command so many times that you want it run at every time you start a powershell windows.
There are several profiles that can be loaded depending on how powershell is started. And there are also global policies for all users of a computer.
Variable | |
---|---|
$PROFILE.AllUsersAllHosts | |
$PROFILE.AllUsersCurrentHost | |
$PROFILE.CurrentUserAllHosts | |
$PROFILE.CurrentUserCurrentHost |
AllHosts are run for all types of Powershell, both regular console and ISE sessions. CurrentHost runs just for that specific so you can have different settings for ISE and console sessions.
The basic structure for the profiles are:
- Locations:
- Current user:
$([environment]::getfolderpath(“mydocuments”))\WindowsPowerShell
- All users:
$($env:systemroot)\System32\WindowsPowerShell\v1.0
- Current user:
- Filenames:
- All types:
profile.ps1
- Console:
PowerShell_profile.ps1
- ISE:
PowerShellISE_profile.ps1
- All types:
Since I am usually have more than one Powershell at a time running with alternative credentials I had a hard time remember which windows was which.
Of course I could have just run whoami
, but that is also more work than needed. So I decided that placing the Username in the title was the way to go.
This is also a good place to place other functions that you have written and you call all the time.
Profile I am using
$host.ui.RawUI.WindowTitle = "$($host.ui.RawUI.WindowTitle) ($([System.Environment]::UserdomainName)\$([system.environment]::UserName))"
What I do I take the current line and tag on the UserDomain and Username. The following line does the same output, but this one relies on an external binary (whoami). There is one drawback, it will lowercase the domain.
$host.ui.RawUI.WindowTitle = "$($host.ui.RawUI.WindowTitle) ($(whoami))"
If I want to build the prompt from scratch without reusing the old prompt to check if I am Administrator or not I use the following:
#Check if UAC has hidden permissions
$myIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$wp = New-Object Security.Principal.WindowsPrincipal($MyIdentity)
if ($wp.IsInRole([Security.Principal.WindowsbuiltInRole]::Administrator))
{ $uac = 'Administrator: ' }
else
{ $uac = '' }
$host.ui.RawUI.WindowTitle="$uac$([System.Environment]::UserdomainName)\$([system.environment]::UserName)"
Remove-Variable wp, myIdentity, uac