James
James

Reputation: 1

Powershell Script to change registry

I am in need of a little help. I am trying to write a script that will search the HKU and HKCU keys for the values "SMTP server" and "SMTP use auth" then replace the values with new values.

So far the way I am doing it is

$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 registery for the string value.  Store 
#Registry Keys in out-file
Get-ChildItem HKU:\ -Recurse -ErrorAction SilentlyContinue |   
    ForEach-Object {   
  if((get-itemproperty -Path $_.PsPath) -match $searchString)  
  {   
   $_.PsPath  
  }   
} | out-File HKU_email.txt
# Write a message to let the user know the script completed. 
Write-Host "Part1 has completed."
# == End of Main Code =======================================
# Run VBS file to modify text files
Start-Sleep -Seconds 2
cscript.exe HKU.vbs
Start-Sleep -Seconds 2
# Run Through the file and set the values below in the registry.
$Dfile = "HKU_Email.txt"
$Fi = Get-Content $dfile
foreach ($data in $Fi) {Set-ItemProperty -Path $data -Name "SMTP Server" -Value [Byte[]](<i>New SMTPSERVER<i>)) -type Binary}
foreach ($data in $Fi) {Set-ItemProperty -Path $data -Name "SMTP Use Auth" -Value "0" -type Dword}
Write-Host "Changes in Registry for HKU Completed"

The VBscript HKU.vbs to parse the file to make the lines change from

Microsoft.PowerShell.Core\Registry::HKEY_USERS\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\[email protected]\9375CFF0413111d3B88A00104B2A6676\00000001

to

HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\[email protected]\9375CFF0413111d3B88A00104B2A6676\00000001

My question is, is there a way to accomplish what I am doing in the VBscript with powershell? I have to run this on about 100 machines ranging from windows xp to win7 32/64bit.

Thank you,

Upvotes: 0

Views: 1142

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Try something like this:

$Fi = (Get-Content $dfile) -replace '^.*?::HKEY_USERS\\', 'HKCU:\'

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201632

Try this:

$a,$b,$path = $_.pspath -split ':'
$newpath = $path -replace 'HKEY_USERS(.*)','HKCU:$1'

This uses features new to PowerShell V3 (-split).

Upvotes: 0

Related Questions