Skip to main content

Set Last Logged In User

Summary

This is an RMM implementation of the agnostic script Set-LastLoggedOnUser to manage the last logged-in user's information displayed on the Windows login screen.

Sample Run

Sample Run Image

  • Select the parameters below to clear the last logged-in user's information from the login screen. The computer must be restarted manually afterward to implement the changes.
    Clear User Info
    Clear User Info

  • Similarly, to clear the last logged-in user's information from the login screen and forcefully restart the computer, select the parameters below.
    Force Restart

  • The parameters below set the specified local user as the last logged-in user. The computer must be restarted manually afterward to implement the changes.
    Set Local User

  • The parameters below set the specified domain user as the last logged-in user and forcefully restart the computer.
    Set Domain User

Dependencies

Set-LastLoggedOnUser

User Parameters

NameExampleRequiredDescription
Clear1TrueClears the last logged-in user's information from the login screen.
UserNameDomain/UserNameFalseSets the specified username as the last logged-in user. The username should be in the format 'Domain/User' or 'User'.
DisplayNameUser NameFalseOptionally specifies the display name to set for the last logged-in user. If not provided, it defaults to the username.
Reboot1FalseOptionally restarts the computer to apply the changes immediately.

Task Creation

Create a new Script Editor style script in the system to implement this Task.

Task Creation

Name: Set Last Logged In User
Description: This script manages the last logged-in user's information displayed on the Windows login screen and can optionally restart the computer to apply changes.
Category: Custom
Task Category

Parameters

Add a new parameter by clicking the Add Parameter button present at the top-right corner of the screen.
Add Parameter

This screen will appear.
Parameter Screen

  • Set Clear in the Parameter Name field.

  • Select Number Value from the Parameter Type dropdown menu.

  • Click the Save button.
    Save Parameter

  • It will ask for confirmation to proceed. Click the Confirm button to create the parameter.
    Confirm Parameter

Add another parameter by clicking the Add Parameter button present at the top-right corner of the screen.
Add Parameter

  • Set UserName in the Parameter Name field.

  • Select Text String from the Parameter Type dropdown menu.

  • Click the Save button.
    Save UserName

  • It will ask for confirmation to proceed. Click the Confirm button to create the parameter.

Add another parameter by clicking the Add Parameter button present at the top-right corner of the screen.

  • Set DisplayName in the Parameter Name field.
  • Select Text String from the Parameter Type dropdown menu.
  • Click the Save button.
  • It will ask for confirmation to proceed. Click the Confirm button to create the parameter.

Add another parameter by clicking the Add Parameter button present at the top-right corner of the screen.

  • Set Reboot in the Parameter Name field.
  • Select Number Value from the Parameter Type dropdown menu.
  • Click the Save button.
  • It will ask for confirmation to proceed. Click the Confirm button to create the parameter.

All the parameters will look like the following:
Parameters

Task

Navigate to the Script Editor Section and start by adding a row. You can do this by clicking the Add Row button at the bottom of the script page.
Add Row

A blank function will appear.
Blank Function

Row 1 Function: PowerShell Script

Search and select the PowerShell Script function.
PowerShell Script

The following function will pop up on the screen:
PowerShell Function

Paste the following PowerShell script and set the expected time of script execution to 900 seconds. Click the Save button.

$Clear = "@Clear@"
$UserName = "@UserName@"
$DisplayName = "@DisplayName@"
$Reboot = "@Reboot@"
#region Setup - Variables
$ProjectName = 'Set-LastLoggedOnUser'
# # Parameters and Globals
# # Be sure that the name of the hashtable property matches the name of the parameter of the script that you are calling.
if ( $Clear -eq 1 ) {
$parameters = @{
Clear = $true
Restart = $Reboot -eq 1
}
} else {
$parameters = @{
UserName = $UserName
DisplayName = if ( $DisplayName -match '[0-9A-z_]' ) { $DisplayName } else { $($UserName -split '\\')[-1] }
Restart = $Reboot -eq 1
}
}
[Net.ServicePointManager]::SecurityProtocol = [enum]::ToObject([Net.SecurityProtocolType], 3072)
$BaseURL = 'https://file.provaltech.com/repo'
$PS1URL = "$BaseURL/script/$ProjectName.ps1"
$WorkingDirectory = "C:\ProgramData\_automation\script\$ProjectName"
$PS1Path = "$WorkingDirectory\$ProjectName.ps1"
$Workingpath = $WorkingDirectory
$LogPath = "$WorkingDirectory\$ProjectName-log.txt"
$ErrorLogPath = "$WorkingDirectory\$ProjectName-Error.txt"
#endregion
#region Setup - Folder Structure
mkdir -Path $WorkingDirectory -ErrorAction SilentlyContinue | Out-Null
$response = Invoke-WebRequest -Uri $PS1URL -UseBasicParsing
if (($response.StatusCode -ne 200) -and (!(Test-Path -Path $PS1Path))) {
Write-Error -Message "No pre-downloaded script exists and the script '$PS1URL' failed to download. Exiting."
return
} elseif ($response.StatusCode -eq 200) {
Remove-Item -Path $PS1Path -ErrorAction SilentlyContinue
[System.IO.File]::WriteAllLines($PS1Path, $response.Content)
}
if (!(Test-Path -Path $PS1Path)) {
Write-Error -Message 'An error occurred and the script was unable to be downloaded. Exiting.'
return
}
#endregion
#region Execution
if ($Parameters) {
& $PS1Path @Parameters
} else {
& $PS1Path
}
#endregion

if ( !(Test-Path $LogPath) ) {
Throw 'PowerShell Failure. A Security application seems to have restricted the execution of the PowerShell Script.'
}
if ( Test-Path $ErrorLogPath ) {
$ErrorContent = ( Get-Content -Path $ErrorLogPath )
throw $ErrorContent
}
Get-Content -Path $LogPath

Save Script

Click the Save button at the top-right corner of the screen to save the script.

Completed Task

Completed Task

Output

  • Script Log