天天看点

powershell修改注册表

Summary: Microsoft Scripting Guy, Ed Wilson, shows to use the PowerShell registry provider to easily modify registry property values.

To modify the value of a registry property value requires using the Set-PropertyItem cmdlet.

Only the steps…

Modifying the value of a registry property value:

Use the Push-Location cmdlet to save the current working location.

Use the Set-Location cmdlet to change to the appropriate registry drive.

Use the Set-ItemProperty cmdlet to assign a new value to the registry property.

Use the Pop-Location cmdlet to return to the original working location.

In the image that follows, a registry key named hsg exists in the HKCU:\Software hive. The registry key has a property named NewProperty.

<a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5773.hsg_2D00_5_2D00_12_2D00_12_2D00_01.png" target="_blank"></a>

When you know that a registry property value exists, the solution is really simple. You use the Set-ItemProperty cmdlet and assign a new value. The code that follows saves the current working location, changes the new working location to the hsg registry key, uses the Set-ItemProperty cmdlet to assign new values, and then uses the Pop-Location cmdlet to return to the original working location.

The code that follows relies on positional parameters for the Set-ItemProperty cmdlet. The first parameter is Path. Because the Set-Location cmdlet sets the working location to the hsg registry key, a period identifies the path as the current directory. The second parameter is the Name of the registry property to change. In this example, it is NewProperty. The last parameter is Value, and that defines the value to assign to the registry property. In this example, it is mynewvalue. Thus, the command with complete parameter names would be:

Set-ItemProperty -Path . -Name newproperty -Value mynewvalue. The quotation marks in the code that follows are not required, but they do not harm anything either.

Here is the code:

PS C:\&gt; Push-Location

PS C:\&gt; Set-Location HKCU:\Software\hsg

PS HKCU:\Software\hsg&gt; Set-ItemProperty . newproperty "mynewvalue"

PS HKCU:\Software\hsg&gt; Pop-Location

PS C:\&gt;

Of course, all the pushing and popping and setting of locations are not really required. It is entirely possible to change the registry property value from any location within the Windows PowerShell provider subsystem.

Only the step…

The short way to change a registry property value:

Use the Set-ItemProperty cmdlet to assign a new value. Ensure that you specify the complete path to the registry key.

Here is an example of using the Set-ItemProperty cmdlet to change a registry property value without first navigating to the registry drive.

PS C:\&gt; Set-ItemProperty -Path HKCU:\Software\hsg -Name newproperty -Value anewvalue

If you need to set a registry property value, you can set the value of that property easily by using the Set-ItemProperty cmdlet. But what if the registry property does not exist? How do you set the property value then? You can still use the Set-ItemProperty cmdlet to set a registry property value, even if the registry property does not exist.

Set-ItemProperty -Path HKCU:\Software\hsg -Name missingproperty -Value avalue

To determine if a registry key exists is easy: Use the Test-Path cmdlet. It returns True if the key exists and False if it does not exist. This technique is shown here.

PS C:\&gt; Test-Path HKCU:\Software\hsg

True

PS C:\&gt; Test-Path HKCU:\Software\hsg\newproperty

False

But unfortunately, this technique does not work for a registry key property. It always returns False—even if the registry property exists. This is shown here.

PS C:\&gt; Test-Path HKCU:\Software\hsg\bogus

Therefore, if you do not want to overwrite a registry key property if it already exists, you need a way to determine if the registry key property exists—and using the Test-Path cmdlet does not work.

One of the cool things about writing the Hey, Scripting Guy! Blog is the interaction with the readers. One such reader, Richard, mentioned the problem of using Test-Path to determine if a registry property exists prior to calling the Set-ItemProperty. This is the technique he suggested, and it works great.

Testing for a registry key property prior to writing a new value:

Use the if statement and the Get-ItemProperty cmdlet to retrieve the value of the registry key property. Specify erroraction (ea is an alias) of SilentlyContinue (0 is the enumeration value).

In the script block for the if statement, display a message that the registry property exists, or simply exit.

In the else statement, call the Set-ItemProperty to create and to set the value of the registry key property.

This technique is shown here.

if((Get-ItemProperty HKCU:\Software\hsg -Name bogus -ea 0).bogus) {'Propertyalready exists'}

ELSE { Set-ItemProperty -Path HKCU:\Software\hsg -Name bogus -Value'initial value'}

The use of this technique appears in the image that follows. The first time, the bogus registry key property value does not exist and it is created. The second time, the registry key property already exists and a message to that effect appears.

<a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6558.hsg_2D00_5_2D00_12_2D00_12_2D00_02.png" target="_blank"></a>

Well, this concludes Registry Week. Tomorrow, I answer a question about how to continue with the spirit of the Scripting Games.

 本文转自 bilinyee博客,原文链接:   http://blog.51cto.com/ericfu/1358737     如需转载请自行联系原作者

<a href="http://blog.51cto.com/search/result?q=powershell+%E6%B3%A8%E5%86%8C%E8%A1%A8" target="_blank">powershell 注册表</a>