最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Setting Local User Image Through Powershell - Stack Overflow

programmeradmin3浏览0评论

I am trying to set the profile picture for the current user from a website link where we store assets. Unfortunately, when I run this, it does not work. It says it does and puts the pictures in the requested location,but does not update the user profile even after logoff/logon. Is there any reason why the user profile isn't updating? Am I missing something?

function Set-ProfilePicture {
    param (
        [string]$ImageURL = ".png"
    )
    try {
        # Get current user info
        $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $currentUsername = $currentUser.Name.Split('\')[-1]
        $currentUserSID = $currentUser.User.Value
        
        Write-Host "Setting profile picture..." -ForegroundColor Yellow
        
        # Create basic paths
        $picturePath = "C:\ProgramData\Microsoft\User Account Pictures\$currentUsername.png"
        
        # Download the image
        try {
            Invoke-WebRequest -Uri $ImageURL -OutFile $picturePath -UseBasicParsing
        }
        
        # Set basic registry entries
        $registryPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\AccountPicture\Users\$currentUserSID"
        
        # Create the path if needed
        if (-not (Test-Path $registryPath)) {
            New-Item -Path $registryPath -Force | Out-Null
        }
        
        # Set the main image paths - simplest possible approach
        Set-ItemProperty -Path $registryPath -Name "Image192" -Value $picturePath -Type String -Force
        Set-ItemProperty -Path $registryPath -Name "Image448" -Value $picturePath -Type String -Force
        
        # Restart the User Profile Service - this seems to be essential
        $service = Get-Service -Name "ProfSvc" -ErrorAction SilentlyContinue
        if ($service) {
            Restart-Service -Name "ProfSvc" -Force -ErrorAction SilentlyContinue
            Start-Sleep -Seconds 2
        }
        
        # Notify Windows
        rundll32.exe user32.dll, UpdatePerUserSystemParameters
        
        Write-Host "Profile picture has been set." -ForegroundColor Green
        Write-Host "Note: System restart recommended for changes to take effect." -ForegroundColor Cyan
    }
    catch {
        Write-Host "Error setting profile picture: $($_.Exception.Message)" -ForegroundColor Red
    }
}

I'm relatively new to this and I've been grabbing code snippets as I go so I can try to make something work.

I've also had an instance with an older version that was much longer unnecessarily. I cleaned it down to what I have above.

I can't seem to find anything for a non-AD or AAD user local account profile.

发布评论

评论列表(0)

  1. 暂无评论