Reg unload and new key

I can run these commands and everything is expected

reg load HKU\Kayla C:\Users\Kayla\ntuser.dat
New-Item -Force Registry::HKU\Kayla\Foo

However, doing this after an error

PS > reg unload HKU\Kayla
ERROR: Access is denied.

If I manually open the registry editor, I can unload the bush, but I would, for example, unload from the script, if possible.

Update: after reading Matts answer, I found that it works if you run the command before collecting, example

0
[gc]::collect()

It seems to 0act like a “Recycle Bin”, but collectit is “Permanent Delete”.

+4
source share
3 answers

2015-04-07: , , PowerShell . .


, New-Item , , GC , script, ( " → DLL...", .)

, New-Item :

$result = New-Item # ...
$result.Handle.Close()

[gc]::Collect() reg unload.

PowerShell, , - GC Debug Release, [gc]::WaitForPendingFinalizers(), , , , . Release , .

SOME_USER:

$path = "HKLM:\TEMP_hive\newkey" # Key we're going to create.
reg load HKLM\TEMP_hive C:\Users\SOME_USER\NTUSER.DAT
$result = New-Item -Path $path
$result.Handle.Close()
[gc]::Collect()
[gc]::WaitForPendingFinalizers() # Optional, and beware of deadlocks! Only seen this needed in Debug mode.
reg unload HKLM\TEMP_hive

, .

:

[gc]::Collect() , script, New-Item. , , , PowerShell ISE, [gc]::Collect(), , , . PowerShell ISE :

$path = "HKLM:\TEMP_hive\differentnewkey" # Key we're going to create.
reg load HKLM\TEMP_hive C:\Users\SOME_USER\NTUSER.DAT
New-Item -Path $path

:

[gc]::Collect()
reg unload HKLM\TEMP_hive

reg unload .

, New-Item , $result, , , $ reg unload . :

$path = "HKLM:\TEMP_hive\thirdnewkey" # Key we're going to create.
reg load HKLM\TEMP_hive C:\Users\SOME_USER\NTUSER.DAT
$result = New-Item -Path $path

:

[gc]::Collect()
reg unload HKLM\TEMP_hive

, $result.Handle.Close().

, , , , ( + , ) - , . , - , .

+2

, , . ( )

[gc]::collect()

Collect GC .NET, .

, . , Get-ChildItem variable: $. , -, . . .

Get-ChildItem variable:

Name                           Value                                                     
----                           -----                                                     
$                              HKU\Kayla   

, , , , .

PS C:\Windows\system32> reg load HKU\Kayla C:\temp\file.dat
The operation completed successfully.
PS C:\Windows\system32> New-Item -Force Registry::HKU\Kayla\Foo

    Hive: HKU\Kayla

Name                           Property
----                           --------
Foo

PS C:\Windows\system32> reg unload HKU\Kayla
ERROR: Access is denied.
PS C:\Windows\system32> Get-ChildItem variable:

Name                           Value
----                           -----
$                              HKU\Kayla

PS C:\Windows\system32> Get-ChildItem variable:

Name                           Value
----                           -----
$                              variable:

PS C:\Windows\system32> reg unload HKU\Kayla
The operation completed successfully.
+1

, [Microsoft.Win32.Registry]:: SetValue, . . Microsoft .

0
source

All Articles