通过PowerShell创建MyDocuments快捷方式的Azure Intune执行异常问题
The issue occurs because when your script runs via Azure Intune, it executes in the SYSTEM account context, not the end user's context. The [environment]::GetFolderPath('MyDocuments') cmdlet returns the Documents folder of the SYSTEM account (located at C:\Windows\System32\config\systemprofile\Documents), which isn't visible to regular users—so File Explorer redirects this to "This PC" when accessed.
To resolve this, you need to create a shortcut that dynamically points to the current user's Documents folder regardless of who clicks it. The best way to do this is using Windows' special shell folder path shell:Personal, which automatically resolves to the logged-in user's Documents directory (even if it's redirected to OneDrive or a network share).
Here's the corrected script:
# Define the shortcut path in the Public Desktop (visible to all users) $ShortcutPath = "$env:Public\Desktop\Docu.lnk" # Create the WScript Shell object $WshShell = New-Object -ComObject WScript.Shell # Create the shortcut $Shortcut = $WshShell.CreateShortcut($ShortcutPath) # Use shell:Personal to target the current user's Documents folder $Shortcut.TargetPath = "shell:Personal" # Save the shortcut $Shortcut.Save()
Why this works:
shell:Personalis a shell namespace path that Windows interprets per-user. When any user clicks the shortcut, Windows resolves it to their own Documents folder, not the SYSTEM account's.- This approach is more reliable than hardcoding paths because it adapts to any folder redirection or custom locations the user might have set.
Additional Notes:
- If you need to create the shortcut only for specific users (instead of all users), replace
$env:Public\Desktopwith the target user's desktop path. However, since Intune runs as SYSTEM, you'd need to first retrieve the user's profile path (e.g., via querying the registry for their SID). - Always test the script in a virtual machine or test environment before deploying it to production via Intune.
内容的提问来源于stack exchange,提问作者G.Arefin

