12

How do I use a function in my profile on the remote machine when using Enter-PSSession on my local machine to open a remote PowerShell session.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
general exception
  • 4,202
  • 9
  • 54
  • 82

5 Answers5

31

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://learn.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://learn.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

Community
  • 1
  • 1
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • "Additionally, only the machine-level profile in $pshome is loaded." Has this changed at some point? With Windows 8.1 and WIndoes Server 2012, Enter-PSSession is not running any Powershell profiles on the target machine. – Greenstone Walker May 14 '14 at 23:17
  • @GreenstoneWalker - I'm seeing this too. With a Win 2008 R2 / PS 3.0 server. – smaglio81 Aug 07 '14 at 18:41
  • 1
    If you don't want to depend on a specific windows version or language, use: `[Environment]::GetFolderPath('MyDocuments')` – Mark Toman Nov 25 '16 at 20:42
7

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.

Ethaan
  • 11,291
  • 5
  • 35
  • 45
  • 1
    This works, but PowerShell has a few bugs that stop it from working well. First, there's no way to wait on "Enter-PSSession -Session", which mean as soon as you call this statement you have spawed a new process in the same window with no wait to spinlock in the script until a users types exit. Invoke-command -session, runs in parallel not in serial...so its a race condition.. Better to do this with the other method and never use "New-Session"... If microsoft had a mode that ensured that each invoke command was serially executed... – pico Oct 22 '21 at 16:24
  • @pico, This isn't intended for use in a script. It should Mimic using ssh on a linux host. Also, Invoke-Command runs synchronously, and is not a Race Condition. Notice I'm using New-Session only one time, and then use Invoke and Enter on the same object. You need multiple sessions to get concurrency. j.ps1 Contents: `$id = (New-Guid).Guid; $sleep = Get-Random -Min 1 -Max 10; "Job $id sleep-time: $sleep"` `0..5 | % {"$id : $_";sleep $sleep}` Test Script `$session = New-PSSession;` `0..10 | %{ Invoke-Command -FilePath "j.ps1" -Session $session}` Results indicate serial. – Devon Dieffenbach Nov 30 '21 at 12:49
1

take a look at this

http://jrich523.wordpress.com/2010/07/08/creating-a-profile-for-a-remote-session/

its a work around for creating a remote profile.

Justin
  • 21
  • 1
  • 3
0

You can't. 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 remote functions available you'll have to initialize them in the startup script of the remote session configuration. Have a look at get/set-pssessionconfiguration on the remote server.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • Did something change in the way these things are handled? I'm with powershell 5.1 and I created a profile.ps1 in C:\Windows\System32\WindowsPowerShell\v1.0\ just to set an alias, which is working when using the shell locally. But when connecting to that machine using Enter-PSSession, the alias is not working... – Alex Jul 31 '17 at 21:45
  • @Alex Perhaps you may need to restart the WinRM service on the server end? A reboot should do it, but if this is a little heavyweight then "restart-service winrm" should work. Or, things may have changed as you say... – x0n Aug 07 '17 at 01:56
0

There is another way to use a profile on a remote session:

  1. Copy the chosen profile to the remote server(s) in your documents folder. For example:

Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents

  1. Enter the PSSession

Enter-PSSession -ComputerName ComputerName

  1. Dot source your profile

. .\Profile.ps1

Drawbacks of this solution:

  • you must copy once your profile to all the computers where you want to have a profile
  • you must dot source the profile every time you enter a PSSession

Advantages of this solution:

  • There is no need of function and you just enter a PSSession like you always do
  • You do not alter all users' profile by changing the default session configuration

(Thanks to Jeffery Hicks for the tip about the dot sourcing)

Luke
  • 1,734
  • 2
  • 15
  • 18