Delete OE Identity from HKCU\Identities using WSH Script

  • Thread starter Thread starter Mona Syed
  • Start date Start date
M

Mona Syed

Hello,

I have the need to delete the "Main Identity" which is the default identity
that Outlook Express creates at HKCU\Identities. It is Registry key that is
represented by the GUID {xxxxxxxx-xxxxx-xxxx-xxxxxxx-xxxx}. I have to do
this using a WSH script. Does anyone have the code (similar to) to traverse
the registry keys to find the Identities key in HKCU (GUID) whose User Name
value is "Main Identity" and then delete that key.

Thanks in advance for your help.
 
Mona said:
I have the need to delete the "Main Identity" which is the default identity
that Outlook Express creates at HKCU\Identities. It is Registry key that is
represented by the GUID {xxxxxxxx-xxxxx-xxxx-xxxxxxx-xxxx}. I have to do
this using a WSH script. Does anyone have the code (similar to) to traverse
the registry keys to find the Identities key in HKCU (GUID) whose User Name
value is "Main Identity" and then delete that key.

Hi

Here you go:


Option Explicit
Dim oReg, sBaseKey, iRC, sKey, aSubKeys, sValue

Const HKCU = &H80000001 'HKEY_CURRENT_USER
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& "./root/default:StdRegProv")

sBaseKey = "Identities\"

iRC = oReg.EnumKey(HKCU, sBaseKey, aSubKeys)
If iRC = 0 Then
For Each sKey In aSubKeys
oReg.GetStringValue HKCU, sBaseKey & sKey, "Username", sValue
If LCase(sValue) = "main identity" Then
DeleteRegistryKey HKCU, sBaseKey & sKey
End If
Next
End If


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub
 
Torgier, back from vacation and just saw this. Thank you so very much for
helping. I haven't run it yet but it looks like it does exactly what I
need. Thanks again.
 
Torgier,

This script works for me (in Windows XP and Windows 2000), but it gives me a
syntax error when I use it on Windows NT machines on line:
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& "./root/default:StdRegProv")
I tried it witout the impersonalationLevel also "Set oReg =
GetObject("winmgmts:root/default:StdRegProv")", but get the same syntax
error.

I read that for WMI to work on Windows NT, there is a special download from
WMI SDK. This is NOT an option in our shop. So my question is, is there an
alternate way to do the same thing (delete Outlook's default "Main
Identity") without making use of WMI.
 
Mona said:
This script works for me (in Windows XP and Windows 2000), but it gives me a
syntax error when I use it on Windows NT machines on line:
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& "./root/default:StdRegProv")
(snip)
I read that for WMI to work on Windows NT, there is a special download from
WMI SDK. This is NOT an option in our shop. So my question is, is there an
alternate way to do the same thing (delete Outlook's default "Main
Identity") without making use of WMI.

Hi

Note that if you want to use WMI to delete registry key later on for Win2k and
WinXP, you should use the updated DeleteRegistryKey sub here:

http://groups.google.com/[email protected]


And yes, you are correct, WMI is not available in NT4 unless you install the WMI
core yourself.

Without using WMI or any other 3rd party tool it will be more difficult, but not
impossible.

A non-WMI solution that will work on all OS's (Win9x, NT4, Win2k and up):

From your vbscript run regedit.exe to export the HKCU\Identities branch to a
file and then parse the file to find the name(s) of the subkey(s) whose User
Name value data is "Main Identity". You can then create and use a temporary
registry file to delete a key with any sub-keys with.

Regedit command-line switches:
http://groups.google.com/[email protected]

This will export a registry branch to a ASCII file:
regedit.exe /S /E:A "some file.reg" "some key"

Here is a starting point at least for the export to file part:
http://groups.google.com/[email protected]


You can delete a registry key with any sub-keys with a registry file:

http://groups.google.com/[email protected]

You can use this registry file from a vbscript file as well, using the
FileSystemObject to create the registry file on the fly and then shell out
(using the Run method) and run regedit.exe /s "some reg file":

http://groups.google.com/[email protected]
 
You may use WScript.Shell object (follow is an example from MSDN):


Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY"
WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\MindReader", "Goocher!",
"REG_SZ"

bKey = WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\")
WScript.Echo WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\MindReader")

WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\MindReader"
WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\"
WshShell.RegDelete "HKCU\Software\ACME\"


//------------------------------------
Regards,
Vassiliev V. V.
http://www-sharp.com -
use .Net framework from compiled HTA
 
Viatcheslav V. Vassiliev said:
You may use WScript.Shell object (follow is an example from MSDN):

Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY"
WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\MindReader", "Goocher!",
"REG_SZ"

bKey = WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\")
WScript.Echo WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\MindReader")

WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\MindReader"
WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\"
WshShell.RegDelete "HKCU\Software\ACME\"

Hi

Take a look under the HKCU\Identities registry key and you will see that this is
not a viable solution for Mona (even if the GUID's under the key had been
known).

You have no control of all the key names in that registry branch (and to use
WshShell.RegDelete to delete a registry branch you need to have full control of
all the key names available/possible).
 
Sorry, I thought deleting known registry key is enough.

//------------------------------------
Regards,
Vassiliev V. V.
http://www-sharp.com -
use .Net framework from compiled HTA
 
Viatcheslav V. Vassiliev said:
Sorry, I thought deleting known registry key is enough.

Hi

Well, it depends on what OS version you are running ;-)

If you use WshShell.RegDelete to try to delete a key (that have subkeys)
on Win9x/WinME, it will succeed, but not on a NT based OS.

Different API implementation is the somewhat lame excuse from Microsoft
on this issue, but how difficult could it be to add a builtin recursive
enumeration in the RegDelete method on a NT based OS? We have created our
own DLL (C++) that we can use from vbscript with a RegDelete method that
does just that, to overcome this limitation (and several others as well ;-)
 
Create custom COM object to work with registry is very easy and I am
surpised that work with registry is so weak in WScript.Shell and that Shell
Lightweight Utility Functions (shlwapi.dll) for registry are not implemented
for script (shell namespaces are implemented for script). Access to registry
with WMI is not the best solution. For similar cases and when performance is
critical I built www-Sharp.ClrHost that can use .Net objects from HTA - .Net
framework is more powerfull than scripting solutions and similar in syntax
to script.

//------------------------------------
Regards,
Vassiliev V. V.
http://www-sharp.com -
use .Net framework from compiled HTA
 
Thank you Torgier and Viatcheslav for all the help. I will post the script
when it works for me.
 
Back
Top