A remote session can be set up with the help of PSSession. The predefined remote session is used by default. However, we can also create our own session configurations and then connect to this new session, e.g. to execute a startup script when the session is established. Let’s dive in.
Scenario: Every time a user logs in via PowerShell Remoting, all logged in users should be displayed
First, we create a new file that calls quser. Quser shows all users that are currently logged in or have established a remote session. This file and its command will be executed every time a user connects to a remote PowerShell session.
# Create file with quser in it
New-Item -ItemType File -Path $home\users.ps1 -Value "quser"
Next, we configure a new PoweRshell configuration with the name users and specify the users.ps1 file that we created before.
# Register New PSSession Configuration
Register-PSSessionConfiguration -Name Users -StartupScript $home\users.ps1
Finally, we need to restart the WinRM service.
Restart-Service WinRM
Let’s do the final check.
Enter-PSSession -ComputerName dc01 -ConfigurationName Users
Hooray!
Categories: PowerShell, Windows 11, Windows Server



