Revisiting the PowerShell Profile

I’ve posted about the PowerShell profile before but I didn’t really go in depth on it, so I wanted to revisit the subject to make sure that I address some of the points that I may have missed.

To find your PowerShell profile location, you can use PowerShell to list it.

Get-ChildItem $profile

This will bring back the directory %UserProfile%\Documents\WindowsPowerShell.  Inside of this folder you will see a file named, Microsoft.PowerShellISE_profile.ps1.

PowerShell Profile Location
PowerShell Profile Location
These are also locations where you may find a PowerShell profile:
  • %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
    • This profile is applied to all users and all shells.
  • %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
    • This profile is applied to all users, but only to the Microsoft.PowerShell shell.
  • %UserProfile%\My Documents\WindowsPowerShell\profile.ps1
    • This profile is applied only to the current user, but affects all shells.
  •  %UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
    • This profile is applied to only the current user and Microsoft.PowerShell shell.

I’m sure you saying right now , “Stuart, are you saying that the PowerShell profile is just a PowerShell script?”  The answer to this, is yes.  The profile is just a PowerShell script that is loaded on the start of PowerShell.

With this Profile, you can put commonly used functions into it making your job easier.  I have a rather large profile that contains some work specific scripts that I run on a daily bases.  The most common is checking for free space on VMware Datastores.

Running PowerShell when you have 2 user accounts

If you have an environment like I do, one where you log on to your workstation with a non-elevated account, you may run into issues running PowerShell commands.  After some research, I put a PowerShell profile on my non-elevated account so that it will close and reopen PowerShell ISE with my elevated account, while prompting for credentials.

set-location $PSHome

    #reference: http://gallery.technet.microsoft.com/scriptcenter/63fd1c0d-da57-4fb4-9645-ea52fc4f1dfb
    $IsElevated = $env:USERDNSDOMAIN
        if ($IsElevated -eq 'DomainName')  
        {  
            try 
            {  
                write-host -ForegroundColor yellow "Relaunching the PowerShell in elevated mode in 3 seconds..."
		$P_ISE = Get-Process -Name powershell_ise                
		start-sleep 3
                Start-Process "$PSHOME\powershell_ise.exe" -Credential "DomainName\UserAccount" -ErrorAction 'stop'
            } 
            catch 
            { 
                write-host -ForegroundColor red "Please make sure this script is launched elevated."  
                break               
            } 
            Stop-Process -Id $P_ISE.Id -Confirm:$False
        }
        else
        {
        write-host -ForegroundColor gray "We are running in elevated mode, we can proceed with launching the tool."
        }

*Note: You may have to do a different comparison that what I have in the script as my non-elevated is in a different domain, than my elevated account.

-Stuart

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.