2

This code works from the Powershell command line but generates an error when run from a script:

$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Set-ItemProperty $key Hidden 1

When I run it from my script, I get the following error:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer : The module 'HKEY_CURRENT_USER' could not be
loaded. For more information, run 'Import-Module HKEY_CURRENT_USER'.
At C:\Users\mybitch\Desktop\VSS-Customize-Desktop.ps1:19 char:6
+ $key=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKEY_CURRENT_US...ersion\Explorer:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoLoadModule

This is a simple assignment...Why would Powershell try to import HKEY_CURRENT_USER as a module? Why is the behavior so different from the Powershell command line?

3
  • 1
    HKCU: is a provider. You can check Get-PSDrive and you will see it. If it is not present PowerShell would attempt to load it in theory. Where are you running this script from? Not task scheduler is it? Commented May 29, 2015 at 18:03
  • I'm simply calling the script from an elevated Powershell command line. Commented May 29, 2015 at 18:07
  • Get-PSDrive shows HKCU is loaded. Why would it try to reload it and then fail? Commented May 29, 2015 at 18:13

2 Answers 2

5

The error message betrays the problem. In your script, apparently you are doing this:

$key = HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer

That is an invalid assignment. You should be doing as you describe:

$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Sign up to request clarification or add additional context in comments.

2 Comments

You are correct. The error was actually a few lines down in my code and was missing the quotes. Thanks!
Glad I could help - btw if someone provides a successful answer, you can click the little check box next to that answer to indicate that the problem is solved.
0

Make sure in the script you have the User Hive being loaded, for example if you are running it from a task scheduler you will need it to load a hive for it to have the effect that you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.