当前位置: 移动技术网 > IT编程>脚本编程>Shell > 使用PowerShell修改注册表

使用PowerShell修改注册表

2017年12月01日  | 移动技术网IT编程  | 我要评论
下面的例子里, powershell修改了注册表键值, 完成了security loop disable, 和loopbackcheck disable. 复制代码 代码

下面的例子里, powershell修改了注册表键值, 完成了security loop disable, 和loopbackcheck disable.


#security loop disable so that you can look at it on the same machine
if(($gchn = get-itemproperty "hklm:\system\currentcontrolset\control\lsa\msv1_0\" -name "backconnectionhostnames" -ea silentlycontinue) -eq $null){
new-itemproperty "hklm:\system\currentcontrolset\control\lsa\msv1_0\" -propertytype multistring -value "$url" -name "backconnectionhostnames"
}else{
set-itemproperty "hklm:\system\currentcontrolset\control\lsa\msv1_0\" -name "backconnectionhostnames" -value ($gchn.backconnectionhostnames+" $url")
}
 
#disable loobback check
if((get-itemproperty "hklm:\system\currentcontrolset\control\lsa\" -name "disableloopbackcheck" -ea silentlycontinue) -eq $null){
new-itemproperty "hklm:\system\currentcontrolset\control\lsa\" -propertytype dword -value "1" -name "disableloopbackcheck"
}

实例给大家了,下面分享一些powershell操作注册表的方法吧

访问注册表键值

在powershell中,用户可以通过类似于hkcu:(作为hkey_current_user)和hklm:(代表hkey_local_matchine)的虚拟驱动器访问注册表键值。
如:dir registry::hkey_local_machine\software
通过这种方式用户可以很容易的复制、粘贴注册表内的键值,用户可以通过下面的命令获取已经注册的文件后缀:
dir registry::hkey_classes_root\.* -name | sort-object

读取注册表键值

在powershell中,用户能够以虚拟驱动器的形式来处理注册表的内容
下面的get-regidtryvalues函数列举存储在一个注册表键值下的所有键值,完整代码如下所示:

 
function get-registryvalues($key) { 
         (get-item $key).getvaluenames() 
}

get-registryvalues hklm:\software\microsoft\windows\currentversion

get-registryvalue读取任意注册表键值并返回其内容,完整代码如下所示:

function get-registryvalue($key, $value) { 
         (get-itemproperty $key $value).$value 
} 
get-registryvalue ' hklm:\software\microsoft\windows\currentversion' ` 


sm_gamesname

写入注册表键值

添加或修改注册表键值在powershell中也是很方便的就可以完成的,下面创建名为set-registryvalue函数用来操作注册表键值,以下是完整的代码:

function set-registryvalue($key, $name, $value, $type="string") { 
 if ((test-path $key) -eq $false) { md $key | out-null } 
    set-itemproperty $key $name $value -type $type 
 } 
  set-registryvalue hkcu:\software\testabc myvalue hello 
  set-registryvalue hkcu:\software\testabc myvalue 12 dword 
  set-registryvalue hkcu:\software\testabc myvalue ` 
([byte[]][char[]]"hello") binary

移除注册表键值

通过remove-item删除目标注册表键,函数remove-registrykey的完整代码如下所示:

function remove-registrykey($key) { 
remove-item $key -force 
}

通过remove-itemproperty函数删除注册表值,完整的代码如下所示:

function remove-registryvalue($key, $value) { 
remove-itemproperty $key $value 
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网