I am trying to run calc app using RunOnce registry key(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce). When the machine is restarted and I login using Enter-PSSession remotely with appropriate credentials, when I check the currently running processes using Get-Process I see that "calc" app has not started yet. Only when I login using RDP does the application run.
As per RunOnce registry key, the command runs when the user logs in and then gets deleted. So when the user logs in using Enter-PSSession, technically it must work right?
Here is the code to generate the RunOnce registry key item for the Current user
function Set-RunOnce
{
[CmdletBinding()]
param
(
#The Name of the Registry Key in the Autorun-Key.
[string]
$KeyName = 'Run',
#Command to run
[string]
$Command = '%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command calc'
)
if (-not ((Get-Item -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce).$KeyName ))
{
New-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
}
else
{
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
}
}
Inspired from here: https://www.powershellgallery.com/packages/WindowsImageConverter/1.0/Content/Set-RunOnce.ps1
I tried invoking Get-Process to check for calc app several seconds after entering the PSSession, but to no avail.