Powershell Remoting Profiles

By JonZ and x0n:

When you use pssessions with the default session configurations, no profile scripts run.

When starting a remote interactive session with Enter-PSSession, a remote profile is loaded. Additionally, only the machine-level profile in $pshome is loaded.

If you want a session to be preconfigured (to load custom functions, snap-ins, modules, etc.), add a profile script to a new sessionconfiguration (for initialize them in the startup script of the remote session configuration).

The Register-PSSessionConfiguration cmdlet creates and registers a new session configuration on the local computer. Use Get-PSSessionConfiguration to view existing session configurations. Both Get-PSSessionConfiguration and Register-PSSessionConfiguration require elevated rights (start PowerShell with the “Run as Administrator” option).

In the target computer, where profile.ps1 contains all your functions:

Register-PSSessionConfiguration -Name WithProfile -StartupScript $PsHome\Profile.ps1

To use this preconfigured session you would type, from the local computer:

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile

or

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile -Credential youradminuser@yourtargetdomain

(where $computername is the hostname of the remote server where you registered the pssessionconfiguration).

A good source on PowerShell remoting is the Administrator's Guide to Powershell Remoting.

References:
Powershell Remoting: Use Functions loaded in Powershell remote Profile? http://jrich523.wordpress.com/2010/07/21/update-creating-a-profile-for-a-remote-session/

Understanding and Using PowerShell Profiles
http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/04/understanding-and-using-powershell-profiles.aspx

About_Profiles (Microsoft Docs) https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles

The six different Windows PowerShell profile paths and use

Current User, Current Host - console
$Home[My ]Documents\WindowsPowerShell\Profile.ps1

Current User, All Hosts
$Home[My ]Documents\Profile.ps1

All Users, Current Host - console
$PsHome\Microsoft.PowerShell_profile.ps1

All Users, All Hosts
$PsHome\Profile.ps1

Current user, Current Host - ISE
$Home[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

All users, Current Host - ISE
$PsHome\Microsoft.PowerShellISE_profile.ps1

Windows PowerShell Profiles
http://msdn.microsoft.com/en-us/library/bb613488%28VS.85%29.aspx

This profile applies to all users and all shells.
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1

This profile applies to all users, but only to the Microsoft.PowerShell shell.
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1

This profile applies only to the current user, but affects all shells.
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1

This profile applies only to the current user and the Microsoft.PowerShell shell.
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PowerShell Core Profiles https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles

This profile applies to all users and all hosts.
$env:ProgramFiles\PowerShell\6\profile.ps1

This profile applies to all users, but only to the current host.
$env:ProgramFiles\PowerShell\6\Microsoft.PowerShell_profile.ps1

This profile applies only to the current user, but affects all hosts.
$env:USERPROFILE\Documents\PowerShell\profile.ps1

This profile applies only to the current user and the Microsoft.PowerShell shell.
$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1


Jason, In my case, I wanted to have my Powershell Profile follow me when I remoted into another computer.

I have created a wrapper function Remote that takes a computername, creates a session, loads your profile into the session, and uses enter-pssession.

Here is the code below:

function Remote($computername){
if(!$Global:credential){
$Global:credential =  Get-Credential
}
$session = New-PSSession -ComputerName $computername -Credential $credential
Invoke-Command -FilePath $profile -Session $session
Enter-PSSession -Session $session
}

You could modify the Invoke-Command -FilePath parameter to take any file of your liking.