convert code to powershell

can someone help me convert this code over to powershell please?
@echo off
setlocal enabledelayedexpansion

for /f "tokens=3 delims=." %%a in ('reg query "HKEY_CLASSES_ROOT\Word.Application\CurVer"') do set reg=%%a

set /a i=0
for %%b in (9 11 12 14 15) do (
  if %%b == %reg% goto setver
  set /a i+=1
)

:setver
set /a n=0
for %%c in (2000 2003 2007 2010 2013) do (
  if !n! == !i! set ver=%%c && goto endloop
  set /a n+=1
)

:endloop
echo Microsoft Office gVersion: %ver%
echo.
endlocal

:end
pause

Open in new window


i want to be able to call the function while connected to the computer (i have the connection part done)

i just want to be able to type like 22 and call a function that will produce the results like this bat file.

What i really would like to do is query that registry key for the version numbers and return the result. I'm not sure how to query the registry in powershell.

Thanks!
bbimisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NinjaStyle82Systems AdministratorCommented:
something like this maybe?

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
$ver = (Get-Itemproperty HKCR:\Word.Application\CurVer)."(default)"
$ver = $ver.Split(".")
$v = switch ($ver[2]) 
    { 
        "9" {"2000"} 
        "11" {"2003"} 
        "12" {"2007"} 
        "14" {"2010"} 
        "15" {"2013"} 
    }
Write-Output "Microsoft Office Version: $v"
Remove-PSDrive HKCR

Open in new window

bbimisAuthor Commented:
works but only problem is it is not getting it from the remote system it is only showing it locally.
i need to pass that to a remote system so i can query the registry on the remote. sorry and thanks!
NinjaStyle82Systems AdministratorCommented:
$remotecomputer = "<remotecomputername>"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('ClassesRoot', $remotecomputer)
$RegKey = $Reg.OpenSubKey("Word.Application\\CurVer")
$ver = $RegKey.GetValue($null)
$ver
$ver = $ver.Split(".")
$v = switch ($ver[2]) 
    { 
        "9" {"2000"} 
        "11" {"2003"} 
        "12" {"2007"} 
        "14" {"2010"} 
        "15" {"2013"} 
    }
Write-Output "Microsoft Office Version: $v"

Open in new window

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

bbimisAuthor Commented:
if it requires authentication do i use the -credentials -creds in this as well?
sorry new at powershell
NinjaStyle82Systems AdministratorCommented:
I would just launch powershell as whatever user has permissions (i also think you need remote registry turned on). You can do this by shift-right clicking the powershell shortcut and running as different user.
NinjaStyle82Systems AdministratorCommented:
just realized i have that extra $ver in there on its own line, was using it for diagnostics, but you can remove it.
bbimisAuthor Commented:
well i can't get it to work remotely. I guess i will just use the bat file :(
i'm having issues passing it the creds
i tried your method above but it will not work for me.
i need to be able to somehow pass the $creds = get-credential
then -Computername $compname -Credientals $creds

but i can't figure out how to use it with the openremotebasekey

thanks!
NinjaStyle82Systems AdministratorCommented:
you opened powershell as something like a domain admin and ran and it didn't work? what error are you getting?
bbimisAuthor Commented:
my computer is on a domain yes. i connect to locations via our wan vpn (asa connections)
they ARE domained.  They so i would entered generally username administrator password "whatever" and then it wold let me in
and i pass that to the program using the get-credential function
errors
NinjaStyle82Systems AdministratorCommented:
I suppose the only way it can be done is via wmi instead of .net remote registry. I will see if I can make something really quick in about an hour.
bbimisAuthor Commented:
thanks for your time. i enjoy learning this but i'm clueless :(
NinjaStyle82Systems AdministratorCommented:
this one uses wmi
$comp = "<compname>"
$cred = Get-Credential
$hkcr = 2147483648
$key = "Word.Application\\CurVer"
$value = $null
$reg = get-wmiobject -ComputerName $comp -list "StdRegProv" -namespace root\default -Credential $cred
$ver = ($reg.GetStringValue($hkcr,$key,$value)).sValue
$ver = $ver.Split(".")
$v = switch ($ver[2]) 
    { 
        "9" {"2000"} 
        "11" {"2003"} 
        "12" {"2007"} 
        "14" {"2010"} 
        "15" {"2013"} 
    }
Write-Output "Microsoft Office Version: $v"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bbimisAuthor Commented:
one question where can i look to understand what the reference 2147483648 is for?
not sure why you are storing that value? is it a default that must be used? Just trying to understand thanks!
bbimisAuthor Commented:
i get the following error
error
NinjaStyle82Systems AdministratorCommented:
any error before that? looks like it didn't get a value for the reg key.

This is where i got that value from:
http://blogs.msdn.com/b/vishinde/archive/2012/09/16/interacting-with-system-registry-using-wmi.aspx

The script seems to be working on my network.
bbimisAuthor Commented:
thanks got it !
NinjaStyle82Systems AdministratorCommented:
sweet, have a good one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.