Return value from ProwerShell to Registry HKLM: Access Denied

472 Views Asked by At

Old Issue: I'm trying to write a script in cmd where PS script sets the system format date as current date.....

::::::::::Edit New Issue: I made a bit of advancement. Now that I've more on completely to PowerShell here is the code:

$a=get-date -UFormat "%s"
<# $env:ElDate = ''
Set-Item -Path Env:\ElDate -Value (get-date -UFormat "%s") #>
$HxVal= [System.Convert]::ToString($a,16) <# '{0:x}' -f $a #>
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name InstallDate -Value $HxVal
reg add 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion' /v InstallDate /d $HxVal /f
return $HxVal

But I'm getting this.

Set-ItemProperty: D:\Download\Scripts\test\GetCDate.ps1:5:1
Line |
   5 |  Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVe … 
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
     | Requested registry access is not allowed.

ERROR: Access is denied.
6267ab4f

How do I write in LocalMachine? Can anyone Help?

Regards,

1

There are 1 best solutions below

0
Amit Gogna On

Thanks everyone! I found the solution. Its just that I was aiming so high that I didn't see besides me. A very simple "Back To School" solution did it.

Run PowerShell as Administrator and execute the ps1 script in it. Now I just have to find an app that'll create its exe in administrative privileges.

Note: In actual this scrips sets the current date as Install date in Window Registry that can be accessed through WMIC OS. We usually install windows via images and that sets the install date as the same date the image was created. So in any case, anyone formatting through image and not informing should go in the Non-Compliance part wherein all the 1000+ Servers had the same date. this code can be pushed through GP for the first time or added to the scripts that are required to finalize the Server after image restoration.

Regards,