Admin Power Level: Intermediate Chapter 10: Run Commands on Other Computers

Enter-PSSession: an interactive remote session

Enter-PSSession opens an interactive session on a remote computer. Your prompt changes, and every command you type runs there until you exit.

Lesson 2 of 2 in Chapter 10: Run Commands on Other Computers. Previous step in this path: Invoke-Command: run a command remotely. 3 learning objectives, 7 examples, and a knowledge check.

Start: what you will learn

Enter-PSSession opens an interactive session on a remote computer. Your prompt changes, and every command you type runs there until you exit.

Learning objectives

  • Open and exit an interactive remote session
  • Explain when an interactive session beats Invoke-Command
  • Run several related commands in one session

Concept in simple words

What it is: Enter-PSSession opens an interactive remote session, which means your prompt changes to show the remote computer, and every command you type runs there until you leave. You are not sending one note and waiting for one reply. You are staying in a live conversation with the remote computer, command after command.

Why it works

Why it exists: Some jobs cannot be planned in one command. You check a service, the answer raises a new question, and that question leads to another check. With Invoke-Command, you would have to send a fresh command for every single step. With an interactive session, you stay connected, read each answer, and decide the next step at once. That exploring style is exactly what an interactive session is for.

Think of it like this

Think of it like this: Invoke-Command is like sending a note and waiting for a reply, while Enter-PSSession is like picking up the phone and staying on the call. On a call, you can ask a question, hear the answer, and then decide what to ask next, without hanging up and dialling again. The call stays open. The session stays open. That is the whole difference.

What happens behind the command: Remoting uses WinRM to carry your typed commands to the remote computer and to bring the results back, and the session keeps that connection open, so you do not need to send a fresh command package for every single step. Your local variables, which are the named values stored on your own computer, do not travel with you, and remote variables do not come back on their own, because each computer keeps its own session state. The session is a shared workspace on the remote computer, not a copy of your computer.

Safety first, stated plainly: Use an interactive session only on a computer you own or manage, and only with permission. Never enter a session on a computer you do not own or manage. An interactive session is even easier to misuse than a single remote command, because every command you type, including a changing command, runs on the remote computer until you exit. Permission and care are not optional here.

How it works, step by step

How it works, step by step: First, check that remoting works and that you have permission on the target computer, then type Enter-PSSession -ComputerName srv1. Watch your prompt change, for example to [srv1]: PS>, which is your sign that you are now working on the remote computer. Do not type a command until you have seen that change, because the prompt is your proof of where you are.

How to read the prompt: A prompt such as [srv1]: PS C:\> has three parts to notice. The name in square brackets, [srv1], is the remote computer. PS tells you that you are in PowerShell. C:\> is the current folder, and it is a folder on the remote computer, not on yours. When the square brackets disappear, you are back on your own computer. Make reading the prompt a habit, before every command, not only at the start.

Paths mean remote paths now: Inside the session, a path such as C:\Windows refers to the remote computer drive, not to your own drive. A file you see on your own computer is not visible from inside the remote session, unless it also exists on the remote computer at that path. If a command reads a file and the result looks wrong, check the prompt first, and then check whether that file path exists on the remote computer. Many confusing results are simply a local path used in a remote session.

Variables inside the session: A variable you set inside the session lives in that session, and later commands in the same session can use it. That is useful for exploring, because you can store a result and reuse it in the next step. But that variable does not travel back to your computer when you exit, and a variable from your computer does not enter the session on its own. If you need a local value inside the session, type it in, or set it inside the session, instead of assuming it is already there. There is no $using: trick here, because you are typing directly at the remote prompt, not sending a script block from home.

Let us walk through an example: After you enter a session on srv1, a command such as Get-ComputerInfo runs on srv1, and Select-Object WindowsProductName keeps just one fact from the result. Look for the remote computer name in the prompt, and look for the product name in the output, then type Exit-PSSession and check that the prompt no longer shows the remote name, which confirms you are back home. The product name in the output describes srv1, not your computer, even though the output appears on your screen.

Let us walk through a second example, a small exploration: Enter a session on srv1, then run Get-Service -Name Spooler | Select-Object Name, Status to check one service. Read the Status. If the service is stopped and you have permission and a good reason to look further, your next command can check a related fact, such as the computer information or another service. Each answer guides the next command. That chain of read, decide, run is the skill an interactive session teaches, and it is the reason you would choose it over sending one fixed command.

Working in a good order: Start with read only commands, which only look and never change anything. Read the result. Then, only if you have permission and a clear reason, move to a command that changes something, and check the prompt one more time before you run it. Exploring in this order protects you twice. First, you learn the facts before you act. Second, you catch a wrong location before a changing command runs on the wrong computer.

What changes if you change one part: Change -ComputerName to a different computer, and every command after you enter runs on that different computer, not on the first one. Change nothing else, and the same typed commands can give different answers, because they read different computers. That is why the target name and the prompt matter more than the command itself. A correct command on the wrong computer is still a wrong action.

Using a saved session: You can also create a session first with $s = New-PSSession -ComputerName srv1, and then enter it with Enter-PSSession -Session $s. In that case, Exit-PSSession steps you out of the interactive part, but it does not delete the saved session object in $s. The session is still there, and you can enter it again, or use it with Invoke-Command. When you are truly done, close it with Remove-PSSession -Session $s. If you entered with -ComputerName only, with no saved session, Exit-PSSession returns you home and that ad hoc session ends.

Cleaning up, step by step: After you exit, run Get-PSSession on your own computer to list the sessions your current PowerShell session knows about. If a saved session is still listed and you no longer need it, remove it with Remove-PSSession -Session $s, or by piping it, for example Get-PSSession | Remove-PSSession to remove all listed sessions. Do not leave sessions open on a shared computer, and do not leave a session open just because you might need it later. Open it again when you need it.

Credentials in an interactive session: If your current account is not enough on the remote computer, add -Credential (Get-Credential) when you enter, for example Enter-PSSession -ComputerName srv1 -Credential (Get-Credential). Get-Credential opens a prompt for a user name and password. Type the password only into that prompt, never as plain text in a command, and never share it. If the prompt asks for credentials you do not have, stop. Do not guess an account, and do not borrow one.

When to use it

When to use it, and when not to: Use an interactive session when you need to explore one computer, try a few related commands, and let each result guide the next step. Do not use it for the same command on many computers, because Invoke-Command does that in one go. Do not use it for a long, repeated job that you will run again tomorrow, because a saved script block with Invoke-Command is clearer and easier to repeat. Do not stay in a remote session longer than you need, because it is easy to forget where you are and run a changing command on the wrong computer.

If the session does not start: First, check the computer name for a spelling mistake. Then, check that WinRM answers, with Test-WSMan -ComputerName srv1, and check that you have permission on that computer. If the prompt never changes to show the remote name, you are not in the session yet, so do not type commands as if you were. A session that never started cannot change a remote computer, but a command typed at your local prompt can change your own computer, so the prompt check protects both sides.

Closing the session the right way: When you are done, type Exit-PSSession, and your prompt returns to your own computer. Always check the prompt before a command that changes something, so you know which computer you are about to change. Closing the console window instead of typing Exit-PSSession can leave the session hanging, so always exit cleanly, and check your prompt before your next command. A clean exit is a habit, not a courtesy. It frees the connection and removes doubt about where your next command will run.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
Enter-PSSession -ComputerName <Computer>

Example 1

Opens an interactive session. The prompt changes to show you are on srv1.

Example 1
Enter-PSSession -ComputerName srv1

Illustrative output: [srv1]: PS C:\>

Example 2

Runs on srv1, shows one fact, then exits back to your own computer. Get-ComputerInfo reads srv1, Select-Object keeps only WindowsProductName, and Exit-PSSession returns you to your local prompt.

Example 2
Get-ComputerInfo | Select-Object WindowsProductName
Exit-PSSession

Illustrative output: WindowsProductName: Windows 11 Education

Example 3

The first line opens the interactive session on srv1, and the prompt changes to show srv1. The second line runs on srv1, reading the Spooler service and keeping only its Name and Status. The third line, Exit-PSSession, ends the session and returns you to your local prompt, so type it before you run any command meant for your own computer.

Example 3
Enter-PSSession -ComputerName srv1
Get-Service -Name Spooler | Select-Object Name, Status
Exit-PSSession

Illustrative output: [srv1]: PS C:\> Get-Service -Name Spooler | Select-Object Name, Status Name Status ---- ------ Spooler Running

Example 4

This is a small exploration in order. The first line enters srv1. The second line checks the Spooler service. The third line checks one computer fact, and you can let the second answer guide whether you need this third check. The last line exits cleanly back to your own computer.

Example 4
Enter-PSSession -ComputerName srv1
Get-Service -Name Spooler | Select-Object Name, Status
Get-ComputerInfo | Select-Object WindowsProductName
Exit-PSSession

Illustrative output: [srv1]: PS C:\> Get-Service -Name Spooler | Select-Object Name, Status Name Status ---- ------ Spooler Running [srv1]: PS C:\> Get-ComputerInfo | Select-Object WindowsProductName WindowsProductName: Windows 11 Education

Example 5

The first line creates a saved session in $s. The second line enters that saved session. The third line, Exit-PSSession, steps out of the interactive part, but it does not delete the saved session. The fourth line, Get-PSSession, lists the saved session, which proves it is still there after you exit. The last line, Remove-PSSession -Session $s, closes the saved session for good.

Example 5
$s = New-PSSession -ComputerName srv1
Enter-PSSession -Session $s
Exit-PSSession
Get-PSSession
Remove-PSSession -Session $s

Illustrative output: Id Name ComputerName State -- ---- ------------ ----- 1 Session1 srv1 Opened

Example 6

Opens an interactive session using the credentials from the Get-Credential prompt. Type the password only into that prompt, never as plain text in a command.

Example 6
Enter-PSSession -ComputerName srv1 -Credential (Get-Credential)

Illustrative output: [srv1]: PS C:\>

Example 7

Inside the session, $env:COMPUTERNAME shows the name of the computer the session is running on, which should be the remote computer, not your own. This is a quick identity check when you are unsure where you are. Exit-PSSession then returns you home.

Example 7
Enter-PSSession -ComputerName srv1
$env:COMPUTERNAME
Exit-PSSession

Illustrative output: SRV1

Nothing on this page executes PowerShell code. The outputs above are illustrative only.

Common mistakes and troubleshooting

Read these before you try the examples, so you can spot the same problems in your own work.

  • Forgetting you are in a remote session, and running a changing command on the wrong computer. Check the prompt first.
  • Expecting local variables to exist in the remote session. They do not travel automatically.
  • Closing the console window instead of typing Exit-PSSession, which can leave the session hanging, so always exit cleanly, and check your prompt before your next command.
  • Using a local file path inside the session and expecting a file from your own computer to be there. Paths inside the session refer to the remote computer.
  • Expecting a variable set inside the session to come back to your own computer after you exit. It stays in the remote session, and it does not travel home on its own.
  • Using an interactive session for the same command on many computers, one by one, when Invoke-Command would do it in one go.
  • Leaving a saved session open after Exit-PSSession, and forgetting that Exit-PSSession only steps out of a saved session. Use Remove-PSSession when you are truly done.
  • Running a changing command without a read only check first, and without checking the prompt. Start with read only commands, check the prompt, and only then run a changing command, on a computer you own or manage, with permission.

Knowledge check

Knowledge check

How do you leave an interactive remote session?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: Enter-PSSession lets you work on one remote computer as if you were sitting at it, step by step, and Exit-PSSession brings you safely back to your own computer. The prompt in square brackets is your location sign. Paths and variables inside the session belong to the remote computer. Start with read only commands, check the prompt before any changing command, exit cleanly, and remove any saved session you no longer need. Use it for exploring one computer, use Invoke-Command for the same check on many computers, and work only on computers you own or manage, with permission.

Next step: AD users: create, read, change, and remove