Last month I showed you how to read the registry in PowerShell. I went through a script that would read the registered owner and organization. Now I'll show you how to change those properties in PowerShell. Here's the script, which is also available for download from my script library. Here's the script, then we'll go through it:
# =====================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1
#
# NAME: Set-RegisteredUser.ps1
#
# AUTHOR: Jeffery Hicks , JDH Information Technology Solutions
# DATE : 9/22/2006
#
# COMMENT: Demonstration script for writing to the registry in PowerShell
# KEYWORDS: PowerShell, Registry
# ======================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1
#
# NAME: Set-RegisteredUser.ps1
#
# AUTHOR: Jeffery Hicks , JDH Information Technology Solutions
# DATE : 9/22/2006
#
# COMMENT: Demonstration script for writing to the registry in PowerShell
# KEYWORDS: PowerShell, Registry
# ======================================================
#Set-RegisteredUser.ps1
$path="HKLM:\Software\Microsoft\Windows NT\CurrentVersion"
$Owner=Read-Host "What is the name of the new owner or leave blank"`
`n"to keep the current owner:"
$Org=Read-Host "What is the name of the new organization " `
`n"or leave blank to keep the current owner:"
$Owner=Read-Host "What is the name of the new owner or leave blank"`
`n"to keep the current owner:"
$Org=Read-Host "What is the name of the new organization " `
`n"or leave blank to keep the current owner:"
#Only set property if something was entered
if ($Owner.length -gt 0) {
Set-ItemProperty -path $path -name "RegisteredOwner" -value $Owner
}
if ($Owner.length -gt 0) {
Set-ItemProperty -path $path -name "RegisteredOwner" -value $Owner
}
#Only set property if something was entered
if ($Org.length -gt 0) {
Set-ItemProperty -path $path -name "RegisteredOrganization" -value $Org
}
if ($Org.length -gt 0) {
Set-ItemProperty -path $path -name "RegisteredOrganization" -value $Org
}
Write-Host "Run Show-RegisteredUser.ps1 to verify the operation."
Like the first script, we need a rference to the registry path that we are going to modify. This is set to the $path variable. Next the script will prompt you for a new owner and organization using the Read-Host cmdlet. If you prefer to leave the value alone, press Enter and leave the value blank. That's because we use an If statement to check what was entered. If nothing was entered the variable $Owner or $Org will have a length of 0 since they will be treated as strings. So, if the length is greater than 0, then we can do something.
Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
In this case, we are going to call the Set-Property cmdlet. Very simply this cmdlet needs a path, the name of the property to change and the new value.
set-ItemProperty -path $path -name "RegisteredOwner" -value $Owner
As far as the cmdlet is concerned, the registry is one giant obect with many properties. Just like editing the registry with Regedit, once you set the key property it's done. The hardest part of using Set-ItemProperty in the registry is simply knowing the property value name.
For the more ambitious reader, I'll leave it to you to come up with a script that combines my two samples into a more effecient script.
Technorati Tags : PowerShell, Scripting, Registry
Hi Jeff, I try to read a binary registry value and i need to set the same Reg Binary value to another key.But i am not able to do so.
Could you guide me in this issue?
Working with binary values is not easy. If you are trying to copy a value from one key to another that might be do-able but I can’t really show you in a comment. If you wouldn’t mind, post something in the PowerShell forum at ScriptingAnswers.com. You’ll get help not only from me but other helpful forum members as well and others will learn something from your question.