locked
Need powershell script to replace a specific value in registry RRS feed

  • Question

  • Hi All,

    I have some customized application servers which have migrated from one datacenter to another datacenter.

    In new datacenter we are changing the hostnames and IP address.

    The migrated server has old hostname entries in the HKLM section of the server, hence I need a PowerShell script using which I can find a specific value (exact match) and replace which all there in the registry and replace with the new name.

    Any input will be really helpful.


    Regards, Deepak Sharma

    Tuesday, August 28, 2018 4:48 PM

All replies

  • Hi sharma_deepaknk,

    Powershell registry find and replace:

    https://community.spiceworks.com/topic/387573-powershell-registry-find-and-replace?utm_source=copy_paste&utm_campaign=growth


    Cheers,

    Martien van Dijk

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Check my Blog: https://windowstechblog.nl

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Tuesday, August 28, 2018 5:05 PM
  • All you need is to go through below documentations.

    Get-Help Invoke-Command -Full

    Get-Help Get-ItemProperty -Full

    Get-Help Set-ItemPropery -Full

    Then give your try, we all are here to answer your question/queries from your attempt. 


    Regards kvprasoon



    Tuesday, August 28, 2018 8:02 PM
  • Hi,

    Thanks for your question.

    About how to replace a specfific value in registry by PowerShell. Please follow the example below.

    $null = New-PSDrive -Name HKU   -PSProvider Registry -Root Registry::HKEY_USERS
    #Enter the string value to search for in the variable below.
    $SearchString = "SMTP Server"
    #==================================================================================
    # Main Code for HKU:
    # Write a message to the user to let them know the script
    # has started.
    Write-Host "Searching: HKU"
    # Search the registry for the string value.  Store
    # Registry Keys in $KHUValues variable
    $HKUValues = Get-ChildItem HKU:\ -Recurse -ErrorAction SilentlyContinue |
    	ForEach-Object
    	{
    	if((get-itemproperty -Path $_.PsPath) -match $searchString)  
    		{
    			$_.PsPath
    		}  
    	}
    # Write a message to let the user know the script completed. 
    Write-Host "Part1 has completed."
    # == End of Main Code =======================================
    # Replace to make it look pretty
    ForEach ($line in $KHKUValues)
    {
    	$line -replace "Microsoft.PowerShell.Core\Registry::" , ""
    }
    # Run Through the $HKUValues variable and set the values below in the registry.
    foreach ($data in $HKUValues)
    {
    	Set-ItemProperty -Path $data -Name "SMTP Server" -Value ([Byte[]](New SMTP Server Name)) -type Binary
    	Set-ItemProperty -Path $data -Name "SMTP Use Auth" -Value "0" -type Dword
    }
    Write-Host "Changes in Registry for HKU Completed"
    
    

    Best Regards,

    Lee


    Just do it.


    Wednesday, August 29, 2018 6:10 AM
  • Hi,

    Thanks for your question.

    About how to replace a specfific value in registry by PowerShell. Please follow the example below.

    $null = New-PSDrive -Name HKU   -PSProvider Registry -Root Registry::HKEY_USERS
    #Enter the string value to search for in the variable below.
    $SearchString = "SMTP Server"
    #==================================================================================
    # Main Code for HKU:
    # Write a message to the user to let them know the script
    # has started.
    Write-Host "Searching: HKU"
    # Search the registry for the string value.  Store
    # Registry Keys in $KHUValues variable
    $HKUValues = Get-ChildItem HKU:\ -Recurse -ErrorAction SilentlyContinue |
    	ForEach-Object
    	{
    	if((get-itemproperty -Path $_.PsPath) -match $searchString)  
    		{
    			$_.PsPath
    		}  
    	}
    # Write a message to let the user know the script completed. 
    Write-Host "Part1 has completed."
    # == End of Main Code =======================================
    # Replace to make it look pretty
    ForEach ($line in $KHKUValues)
    {
    	$line -replace "Microsoft.PowerShell.Core\Registry::" , ""
    }
    # Run Through the $HKUValues variable and set the values below in the registry.
    foreach ($data in $HKUValues)
    {
    	Set-ItemProperty -Path $data -Name "SMTP Server" -Value ([Byte[]](New SMTP Server Name)) -type Binary
    	Set-ItemProperty -Path $data -Name "SMTP Use Auth" -Value "0" -type Dword
    }
    Write-Host "Changes in Registry for HKU Completed"
    
    

    Best Regards,

    Lee


    Just do it.



    What is the point in posting same code in the link shared in previous post ?

    Regards kvprasoon

    Wednesday, August 29, 2018 12:09 PM
  • Hmm, my first attempt does nothing.  The first command turns up empty.

    Get-ChildItem hkcu:key1 | Set-ItemProperty -Name value1 -Value data1

    Wednesday, August 29, 2018 5:26 PM
  • Work in progress.  I wish these commands flowed together more smoothly.  $_.Property is an array of values.

    Get-ChildItem -recurse hkcu:\key1 | foreach { # doesn't process top key
      $path = $_.PSPath
      $_.Property | foreach {
        $name = $_
        $olddata = get-itemproperty -path $path -name $name | select -expand $name
        if ($olddata -eq 'server1') {
          $newdata = 'server2'
          set-itemproperty -path $path -name $name -value $newdata -verbose -whatif
        }
      }
    }

    • Edited by JS2010 Thursday, August 30, 2018 7:39 PM
    Thursday, August 30, 2018 6:16 PM
  • (Get-Item hkcu:\)key1.GetSubKeyNames()

    How to recurse a registry hierarchy.

    $k = Get-Item hkcu:\key1
    $k.GetSubKeyNames()|%{
        $s = $k.OpenSubKey($_)
        $s.GetSubKeyNames()
        $s.GetValueNames()
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 30, 2018 7:04 PM
    Thursday, August 30, 2018 7:03 PM
  • Fyi, this is how powershell displays a registry key in the default view (type Microsoft.Win32.RegistryKey in registry.format.ps1xml).  For get-item or get-childitem, it calls get-itemproperty anyway.  get-itemproperty just returns a pscustomobject.

    "Hive"
    $_.PSParentPath.Replace("Microsoft.PowerShell.Core\Registry::","")

    "Name & Property"
    $result = (Get-ItemProperty -LiteralPath $_.PSPath |
      Select * -Exclude PSPath,PSParentPath,PSChildName,PSDrive,PsProvider |
        Format-List | Out-String | Sort).Trim()
    $result = $result.Substring(0, [Math]::Min($result.Length, 5000) )
    if($result.Length -eq 5000) { $result += "..." }


    • Edited by JS2010 Thursday, August 30, 2018 9:02 PM
    Thursday, August 30, 2018 8:28 PM
  • Really never necessary. Just use the returned object to get further objects.  Remember...PS is an object system.  It knows what to do.


    \_(ツ)_/

    Thursday, August 30, 2018 8:31 PM
  • I think somewhere along the line there should be a separate object for every registry value.

    Thursday, August 30, 2018 8:57 PM
  • I think somewhere along the line there should be a separate object for every registry value.

    There is.  It is called a "property".  "GM" a property to see all of its properties and methods.

    In the registry a "key" is like an object. PowerShell just converts this and adds a lot of tracking properties for internal use.


    \_(ツ)_/

    Thursday, August 30, 2018 9:10 PM