Hey everyone! I'm trying to execute a PowerShell script that copies two .bat files to user desktops, but I'm running into a problem when I try to run it from the command prompt. The script works perfectly when executed directly in PowerShell, but it doesn't do anything when I run it from the command prompt, unless I hardcode the desktop path. I'm using it for deployment through Intune, which is why I need it to work for all users. I'm wondering if there's a specific way to enable access to environment variables when executing from the command prompt. Any insights would be greatly appreciated!
3 Answers
Definitely check that when you're running your PowerShell script from the command prompt, it's executed in the correct context. You might want to look into tools like the PSAppDeployToolkit to streamline your deployments instead of just relying on PowerShell for everything. It could save you a lot of hassle!
If you're experiencing issues with paths, adding some debugging to check if your source path exists can help. You can use a simple test like `if (Test-Path K:...) { "Found" } else { "Not found" }` before the copy command. Also, consider using `$env:USERPROFILEDesktop` for user-specific paths, which might be more reliable.
It sounds like you're hitting a common issue when deploying scripts with Intune. When running scripts via Intune, the execution context is usually SYSTEM, which means it doesn't have access to user-specific paths as you'd expect from a regular user environment. To get around this, you either need to copy files to the public desktop or adjust the execution behavior in Intune depending on whether you're targeting SYSTEM or USER. You've got the right idea about using `CommonDesktopDirectory` or `Desktop` based on the context.

Exactly! Just make sure you don't mix the contexts. Stick to one or the other for reliable results.