设为首页 收藏本站
查看: 637|回复: 0

use powershell create a new registry-Jason

[复制链接]

尚未签到

发表于 2018-9-3 07:52:08 | 显示全部楼层 |阅读模式
  Creating a new registry key by using Windows PowerShell is the same as creating a new file or a new folder. All three processes use the New-Item cmdlet. In addition, you might use the Test-Path cmdlet to determine if the registry key already exists. You may also wish to change your working location to one of the registry drives. If you do this, you might use the Push-Location, Set-Location, and Pop-Location cmdlets. This, of course, is the long way of doing things...
  Only the steps:

  •   Store the current working location by using the Push-Location cmdlet.
  •   Change the current working location to the appropriate registry drive by using the Set-Location cmdlet.
  •   Use the Test-Path cmdlet to determine if the registry key already exists.
  •   Use the New-Item cmdlet to create the new registry key.
  •   Use the Pop-Location cmdlet to return to the starting working location.
  Assigning a default value to an existing registry key:

  •   Use the Set-Item cmdlet and supply the complete path to the existing registry key.
  •   Supply the default value in the Value parameter of the Set-Item cmdlet.
Use New-Item to create and assign a value
  It is not necessary to use the New-Item cmdlet to create a registry key and then to use the Set-Item cmdlet to assign a default value. These steps are combinable to a single command. The following command creates a new registry key with the name hsg1, and it assigns a default value of “default value” to the registry key.
  New-Item -Path HKCU:\Software\hsg1 -Value "default value
Modifying the value of a registry property value
  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.
  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'}
Entering a remote session to create a new registry key

  •   Use the Get-Credential cmdlet to obtain a credential object with rights on the remote computer. Store the returned credential object in a variable.
  •   Use the Enter-PSSession cmdlet to enter a remote Windows PowerShell session on the target computer.
  •   Use the New-Item cmdlet to create the new registry key.
  •   Use the Exit command to leave the remote Windows PowerShell session.
  The commands to obtain credentials, enter a Windows PowerShell session, create a new registry key, and leave the Windows PowerShell session are shown here.
  $credential = Get-Credential -Credential iammred\administrator
  Enter-PSSession -ComputerName sql1 -Credential $credential
  New-Item -Path HKCU:\Software -Name HSG -Value "my default value"
  Exit
Creating remote registry keys on multiple computers
  Entering a remote Windows PowerShell session to create a registry key on multiple computers is a tedious and time-consuming process. To perform a command on multiple machines, it is better to use the Invoke-Command cmdlet.
  Only the steps…
  Using the Invoke-Command cmdlet to create remote registry keys:

  •   Store the server names in a variable.
  •   Store the connection credentials in a variable (use the Get-Credential cmdlet to obtain the credentials).
  •   Use the Invoke-Command cmdlet to run the command against the remote machines. Place the command to be run in the ScriptBlock parameter.
  The following commands create a new registry key on the HKCU drive on three different servers.
  $servers = "hyperv1","hyperv2","hyperv3"
  $credential = Get-Credential -Credential iammred\administrator
  Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {New-Item -Path HKCU:\Software -Name hsg -Value "scripted default value"}
Creating property values
  It is unusual to have a registry key that has only a default property value. In fact, most registry keys contain multiple property values. To create a new property value, use the New-ItemProperty cmdlet. The following command creates a new property value named NewProperty under the previously created HSG registry key.
  New-ItemProperty -Path HKCU:\Software\hsg -Name NewProperty -Value "New PropertyValue"
Getting registry property values
  When run, the command returns information about the registry key property. In fact, this is the same information that is obtained by using the Get-ItemProperty cmdlet. The use of the Get-ItemProperty cmdlet and the associated output from the command are shown here.
  PS C:\> Get-ItemProperty -path HKCU:\Software\hsg -Name newproperty
  PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\hsg
  PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software
  PSChildName  : hsg
  PSDrive      : HKCU
  PSProvider   : Microsoft.PowerShell.Core\Registry
  NewProperty  : New PropertyValue
  The newly created registry property appears in the output under the name of the property. This means that to display only the value of the registry property requires essentially naming the property twice. The first time the registry property appears, it is the name of the registry property to obtain; the second time, it is the property to retrieve. This command and the associated output are shown here.
  PS C:\> (Get-ItemProperty -path HKCU:\Software\hsg -Name newproperty).newProperty
  New PropertyValue


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561798-1-1.html 上篇帖子: powershell常用命令 下篇帖子: PowerShell 2.0 实践(一)操作文件系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表