Windows Server TechCenter >
Windows Server Forums
>
Windows PowerShell
>
Registry - how to test the type of a value
Registry - how to test the type of a value
- How can you see the type of a value to see if it's a REG_DWORD, REG_SZ, etc?
"Get-ItemProperty KEY VALUE" only seems to show: PSPath, PSParentPath, PSChildName, PSDrive, PSProvider, and the actual value.
I see how you can specify the type when doing New-ItemProperty using the -propertyType flag, but I'm not seeing any way to actually check the itemType of an existing registry key/value.
(Preferably without resorting to using .NET/WMI)
Thanks!
Answers
- Yea seems odd this isn't built in a little better. Using .NET registry APIs is one way, or invoke reg.exe as Marco suggests. Below is basic version of a function to do the work in powershell with minimal .NET APIs. Relies on the fact that registry types line up pretty close with .NET types. It can't detect difference btw REG_SZ and REG_MULTI_SZ unfortunately...
function Get-RegValType([String] $path, [String] $valName)
{
switch ((gp $path -Name $valName).$valName.gettype().Name)
{
"String"{"REG_SZ"; break;}
"Int32" {"REG_DWORD"; break;}
"Int64" {"REG_QWORD"; break;}
"String[]" {"REG_MULTI_SZ"; break;}
"Byte[]" {"REG_BINARY"}
default {throw "Unknown type"}
}
}
-Lincoln- Marked As Answer byMarco ShawMVP, ModeratorWednesday, November 11, 2009 11:44 AM
All Replies
- That's a good question... I'm not sure if PowerShell can do this, however it is a good extension that could be added.
reg.exe can be used to query the registry and will return the type.
In the meantime, I'll also ask around. - Yea seems odd this isn't built in a little better. Using .NET registry APIs is one way, or invoke reg.exe as Marco suggests. Below is basic version of a function to do the work in powershell with minimal .NET APIs. Relies on the fact that registry types line up pretty close with .NET types. It can't detect difference btw REG_SZ and REG_MULTI_SZ unfortunately...
function Get-RegValType([String] $path, [String] $valName)
{
switch ((gp $path -Name $valName).$valName.gettype().Name)
{
"String"{"REG_SZ"; break;}
"Int32" {"REG_DWORD"; break;}
"Int64" {"REG_QWORD"; break;}
"String[]" {"REG_MULTI_SZ"; break;}
"Byte[]" {"REG_BINARY"}
default {throw "Unknown type"}
}
}
-Lincoln- Marked As Answer byMarco ShawMVP, ModeratorWednesday, November 11, 2009 11:44 AM
With this you can query local or remote machine registry:
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("localmachine",$ENV:COMPUTERNAME)
$reg.OpenSubKey("SOFTWARE\Microsoft\DataAccess").GetValueKind("FullInstallVer")
$reg.close()Possible values are:
Unknown
String
ExpandString
Binary
DWord
MultiString
QWord
Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar

