Overview
This page provides a sample script required to silently install and activate the Josys browser extension on Windows devices using Microsoft Intune.
This is intended for those who fall under Case C (users do not have administrator privileges on their devices, and their UPN is the same as their email address in Josys) in How to Silently Install and Activate the Extension on Windows Devices Using Intune (2/2).
Target Audience
Administrators planning or executing the mass deployment of the Josys browser extension.
About the Browser Extension
Please refer to the following pages first for information regarding the browser extension.
1. About the Browser Extension
2. How to Use the Browser Extension
Script
- Copy the script below and paste it into Notepad or a similar text editor.
-
Replace YOUR_ORGANIZATION_KEY in
$organizationKeywith your company's Organization Key, which can be found in Josys. - Replace YOUR_DOMAIN.COM in $fallbackEmailDomain with your organization's email address domain.
- Save the file with a name such as "josys-extension-config.ps1".
- Confirm that the file type is "Windows PowerShell script".
- Return to How to Silently Install and Activate the Extension on Windows Devices Using Intune (1/2) - 2. Apply the script to the target devices and proceed with the steps.
<#
.SYNOPSIS
Sets the UserEmail (UPN) and OrganizationKey for Chrome and Edge extensions in the user's registry.
This script is designed to run in the SYSTEM context via Microsoft Intune.
.DESCRIPTION
1. Identifies the currently logged-in user by checking the owner of the 'explorer.exe' process.
2. Retrieves the User Principal Name (UPN) from the IdentityStore registry cache (Plan A).
3. If Plan A fails, generates the email address using the username and a specified domain (Plan B).
4. Writes the 'UserEmail' and 'OrganizationKey' to the HKEY_USERS hive for the active user.
5. If no active user session is found, it exits with an error code (1) to trigger an Intune retry.
.NOTES
- Run Context: System
- Log Path: %TEMP%\SetExtensionPolicy_YYYYMMDD_HHMMSS.log
#>
# ==========================================
# Configuration Settings
# ==========================================
# [MANDATORY] Set the OrganizationKey value here
$organizationKey = "YOUR_ORGANIZATION_KEY"
# [MANDATORY] Set the fallback email domain here (e.g., "example.com")
$fallbackEmailDomain = "YOUR_DOMAIN.COM"
# Relative registry key paths for the specific extensions
$chromeRelativePath = "Software\Policies\Google\Chrome\3rdparty\extensions\moaklgcgokbgplldonjkoochhlefkbjf\policy"
$edgeRelativePath = "SOFTWARE\Policies\Microsoft\Edge\3rdparty\extensions\hjifncajikcdkhlofdjjlhcjoennmdfc\policy"
# ==========================================
# Helper Functions
# ==========================================
# Function to get the active user session (SID and Username) from the explorer.exe process
function Get-ActiveUserSession {
try {
$explorerProc = Get-CimInstance Win32_Process -Filter "Name = 'explorer.exe'" | Select-Object -First 1
# If explorer.exe is not running, the user is not fully logged in or desktop is not ready
if (-not $explorerProc) { return $null }
$owner = Invoke-CimMethod -InputObject $explorerProc -MethodName GetOwner
if ($owner.ReturnValue -ne 0) { return $null }
$domain = $owner.Domain
$user = $owner.User
# Convert User/Domain to SID
$ntAccount = New-Object System.Security.Principal.NTAccount($domain, $user)
$sid = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier]).Value
return @{
UserName = "$domain\$user"
SID = $sid
UserOnly = $user
}
} catch {
Write-Error "Error in Get-ActiveUserSession: $($_.Exception.Message)"
return $null
}
}
# Function to retrieve the UPN (UserEmail)
function Get-UserUPN {
param (
[string]$TargetSid,
[string]$TargetUserName,
[string]$FallbackDomain
)
# [Plan A] Retrieve UPN from IdentityStore cache
try {
Write-Host "Attempting Plan A: Retrieve UPN from IdentityStore cache..."
# Search cached user info based on SID
$regPath = "HKLM:\SOFTWARE\Microsoft\IdentityStore\Cache\*\IdentityCache\*\$TargetSid"
$cacheItem = Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue | Select-Object -First 1
if ($cacheItem -and $cacheItem.UserName) {
$upn = $cacheItem.UserName
Write-Host "Plan A Success: Found UPN '$upn' from registry cache."
return $upn
} else {
Write-Warning "Plan A failed: Could not find UPN in IdentityStore cache."
}
} catch {
Write-Warning "Plan A failed: $($_.Exception.Message)"
}
# [Plan B] Fallback to generated email (Username + Domain)
$fallbackUPN = "$TargetUserName@$FallbackDomain"
Write-Host "Plan B: Fallback to generated email '$fallbackUPN'."
return $fallbackUPN
}
# ==========================================
# Main Execution Logic
# ==========================================
# Set log path with timestamp to avoid overwriting in multi-user environments
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$logPath = "$env:TEMP\SetExtensionPolicy_$timestamp.log"
Start-Transcript -Path $logPath
Write-Host "Script started."
# Validate Organization Key and Fallback Domain
if ([string]::IsNullOrWhiteSpace($organizationKey) -or $organizationKey -eq "YOUR_ORGANIZATION_KEY") {
Write-Error "CRITICAL ERROR: Please set a valid value for `$organizationKey in the script configuration."
Stop-Transcript
exit 1
}
if ([string]::IsNullOrWhiteSpace($fallbackEmailDomain) -or $fallbackEmailDomain -eq "YOUR_DOMAIN.COM") {
Write-Error "CRITICAL ERROR: Please set a valid value for `$fallbackEmailDomain in the script configuration."
Stop-Transcript
exit 1
}
# 1. Identify Target User
$activeUser = Get-ActiveUserSession
if (-not $activeUser) {
# If no active user is found (explorer.exe not running), exit with error code 1.
# This ensures Intune reports a failure and retries the script later.
Write-Error "No active user session found (explorer.exe not running yet). Returning error to trigger retry."
Stop-Transcript
exit 1
}
Write-Host "Target User: $($activeUser.UserName)"
Write-Host "Target SID: $($activeUser.SID)"
# 2. Retrieve UPN
$targetUPN = Get-UserUPN -TargetSid $activeUser.SID -TargetUserName $activeUser.UserOnly -FallbackDomain $fallbackEmailDomain
if (-not $targetUPN) {
Write-Error "Failed to determine UPN. Exiting."
Stop-Transcript
exit 1
}
Write-Host "Final UPN to write: $targetUPN"
# 3. Write to Registry
$policies = @(
@{ Name = "Chrome"; Path = $chromeRelativePath },
@{ Name = "Edge"; Path = $edgeRelativePath }
)
foreach ($app in $policies) {
# Construct the full path to HKEY_USERS\<SID>\...
$fullPath = "Registry::HKEY_USERS\$($activeUser.SID)\$($app.Path)"
Write-Host "--- Processing $($app.Name) ---"
Write-Host "Target Registry Path: $fullPath"
try {
# Create key if it doesn't exist
if (-not (Test-Path $fullPath)) {
New-Item -Path $fullPath -Force | Out-Null
Write-Host "Created registry key."
}
# Write UserEmail
Set-ItemProperty -Path $fullPath -Name "UserEmail" -Value $targetUPN -Type String -Force
if ($?) { Write-Host "Set UserEmail: OK" }
# Write OrganizationKey
Set-ItemProperty -Path $fullPath -Name "OrganizationKey" -Value $organizationKey -Type String -Force
if ($?) { Write-Host "Set OrganizationKey: OK" }
} catch {
Write-Error "Failed to set registry for $($app.Name): $($_.Exception.Message)"
# We continue to the next app even if one fails
}
}
Write-Host "Script execution finished successfully."
Stop-Transcript
exit 0