1
$filePath = "C:\MobiControl\sample.jpg"
2
function Get-CurrentUser() {
3
try {
4
$currentUser = (Get-Process -IncludeUserName -Name explorer | Select-Object -First 1 | Select-Object -ExpandProperty UserName).Split("\")[1]
5
if (-NOT[string]::IsNullOrEmpty($currentUser)) {
6
Write-Output $currentUser
7
}
8
}
9
catch {
10
Write-Output "Failed to get current user."
11
Write-Host "Error occured while running script -> ",$_.Exception.Message
12
}
13
}
14
function Get-UserSID([string]$fCurrentUser) {
15
try {
16
$user = New-Object System.Security.Principal.NTAccount($fcurrentUser)
17
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier])
18
if (-NOT[string]::IsNullOrEmpty($sid)) {
19
Write-Output $sid.Value
20
}
21
}
22
catch {
23
Write-Output "Failed to get current user SID."
24
Write-Host "Error occured while running script -> ",$_.Exception.Message
25
}
26
}
27
Write-Host "--------------starting script execution--------------"
28
$currentUser = Get-CurrentUser
29
$currentUserSID = Get-UserSID $currentUser
30
$userRegistryPath = "Registry::HKEY_USERS\$($currentUserSID)\Control Panel\Desktop"
31
Set-ItemProperty -Path $userRegistryPath -Name wallpaper -Value $filePath
32
Write-Host "--------------script execution completed successfully--------------"
33
Write-Host "Apply Restart device action to reflect the changes immediately"