q105: Running a PowerShell script fails on Windows
If you run into an error like this while running a script:
File cannot be loaded because running scripts is disabled on this system.
Specific symptom
> .\get_hdwkey.ps1 .\get_hdwkey.ps1 : File C:\Users\lxkt\Desktop\PWmatLICENSE_win\get_hdwkey.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\get_hdwkey.ps1 + ~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS C:\Users\lxkt\Desktop\PWmatLICENSE_win>
(The lxkt in the path above is just the username on the example computer — your own computer will show your own username, which is normal and unrelated to the cause of the error.)
This happens because Windows, for security reasons, disables running unsigned PowerShell scripts by default. It's a very common situation — just follow the steps below to fix it.
Fix
Method 1: Change the execution policy (recommended — fixes it once and for all)
Run PowerShell as administrator (search for PowerShell in the Start menu, right-click it, and choose “Run as administrator”), then run:
# Allow running local scripts Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # You'll need to type ''Y'' to confirm or Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
After running the first command, if it asks for confirmation, type Y and press Enter; or you can use the second command with -Force, which confirms automatically without asking. Once this is set, open a new terminal window (doesn't need to be administrator) and run the original script again — it should work normally.
Method 2: Bypass temporarily (this run only)
If you'd rather not change the system setting permanently, you can bypass it just for this one run instead:
powershell -ExecutionPolicy Bypass -File .\get_hdwkey.ps1 or powershell -ExecutionPolicy Unrestricted -File .\get_hdwkey.ps1
Note that this only applies to this one run — the next time you run a different script, you may hit the same error again. If that's annoying, we'd recommend using Method 1 to fix it once and for all.
