Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Writing the Registry in PowerShell

Posted on September 22, 2006August 5, 2009
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
# ======================================================
#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:"
#Only set property if something was entered
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
}
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!
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.


Behind the PowerShell Pipeline
Technorati Tags : PowerShell, Scripting, Registry

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Writing the Registry in PowerShell”

  1. Anonymous says:
    September 11, 2008 at 1:58 am

    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?

  2. Jeffery Hicks says:
    September 12, 2008 at 11:26 am

    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.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d