Using Profiles in PowerShell

A PowerShell profile is a way to add resources to your session each time you start PowerShell. The most common things to put into a profile include: functions, powershell drives, variables, and importing of modules. Anything you add to your profile will automatically be available to you the next time you start a PowerShell session.

Creating a Profile


In order for PowerShell to pickup a profile you need to create it in the right place. There are actually multiple places you can create a profile depending on the scope in which you want it to take effect. I'll discuss scope later, so first lets take a look at the built-in "$profile" variable. This variable contains the path to the PowerShell profile unique to the currently logged in user. Therefore, you can add code in this profile and other users won't see it. If you output this variable in a PowerShell session you will see a path similar to the following depending on your version of Windows:

                                        
 PS C:\Users\Phil> $profile
 C:\Users\Phil\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

                                        

Also note that if you are inside PowerShell ISE you will get a different profile path unique for ISE. If the $profile path doesn't exist you will need to create it with the New-Item cmdlet or manually in File Explorer. Once it exists you can edit it easily by passing the $profile variable to notepad.

                                        
 #
 # Create the profile if it doesn't exist
 #
 New-Item -Type file -Path $profile

 #
 # Open the profile in PowerShell
 #
 notepad $Profile

                                        

Now that you have a profile you can add any custom functions you have or import PowerShell modules that you like to use. I like to add PowerShell drives that point to network shares.

Profile Scope


The next thing to consider is the scope of the profile. Up until now the profile you created will only affect your sessions, however you can also create a profile that will affect all users and hosts. You can use the following code to list the path locations and their scope:

                                        
 PS C:\Users\Phil> $profile | Format-List -Force


 AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
 AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell \v1.0\Microsoft.PowerShell_profile.ps1
 CurrentUserAllHosts    : C:\Users\Phil\Documents\WindowsPowerShell\profile.ps1
 CurrentUserCurrentHost : C:\Users\Phil\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

                                        

To get a full explanation of each of these paths use "Get-Help about_profiles". The "AllUsersAllHosts" profile is great if you want to make a common set of functions or modules available to all users. Overall, profiles are a great way to centralize code an ensure that the same resources are available to users.


© 2024 Embrs.net