My script asks for a folder name and a delay, then tries to save five screenshots on the desktop. New-Item reports access denied when it creates the folder, and saving the image fails afterward. This is a school VM and I don't have admin access. What should I check?
Why can't my PowerShell screenshot script write to the desktop in a school VM?
Answers
First check where Windows thinks your desktop is: [Environment]::GetFolderPath('Desktop'). A school VM may redirect it, so building a path under C:\Users yourself might point somewhere different.
Use the returned path with Join-Path to add the folder name, then try creating that folder. The correct desktop path can still be restricted, so if you get access denied there too, ask the school IT team where your account is allowed to save files.
Reply to this answer
For a quick test, try creating the output folder under $env:TEMP instead. It is meant for temporary files, though access can still be restricted and files there may be cleaned up. If that works, it narrows the problem down to the desktop location or its permissions.
Reply to this answer
There are a couple of script issues to check after the path problem. The folder prompt in the example is missing its closing quote, and the loop has empty braces, so the screenshot code isn't inside it. Put the capture, save, and sleep statements inside the loop if you want five captures.
Also check the exact spelling of the variable and type names in your file. For example, $env:Username has no space after the colon, and System.Windows.Forms has no spaces in the name. Some of that may just be formatting in the example, but PowerShell won't treat the spaced versions the same way.
Reply to this answer
The save error may just be a consequence of the folder creation failing. Check that New-Item succeeds before trying to save the bitmap, and show the full save error if it still fails after that.
Changing the execution policy won't grant permission to write to a folder. Since this is a managed VM, don't assume you can fix a blocked location by running as administrator.
Reply to this answer
Add your answer
Answers are reviewed before publication.
gently~offcourse_6:
Move anything you need to keep to an approved location rather than relying on the temp folder for storage.